Friday, May 22, 2015

Custom ActionFilter in ASP .Net MVC

I had a scenario like if a person doesn't have proper access to a resource he will receive a message on a page.
Initially I thought I should implement custom Authorization Filter [customize the existing Authorization Filter] - but it looked like Authorization filter is implemented [at least in my application] more like if the filter is not met you are redirected to login page - kind of session checking and stuffs like mandatory things for each of the Actions.
But in my case I was putting this check on a single Action - off course having option to reuse the filter later.
Following is the Controller with Filter:

[Authorize]
[PermittedSiteActionAttribute]
public ActionResult Siteinfo(int id = 0, string nodeType = "")
{
RASSiteDetailsModel model = null;
return View(model);
}

The  PermittedSiteActionAttribute lokked like :
public class PermittedSiteActionAttribute : FilterAttribute, IActionFilter
    {
        bool _IsAuthorised = false;
        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                if (!_IsAuthorised)
                {
                    filterContext.Controller.ViewData.Model = null;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error in PermittedSiteActionAttribute>OnActionExecuted", ex);
            }
        }
        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                var actionParams = filterContext.ActionParameters;
                object siteId;
                actionParams.TryGetValue("id", out siteId);
                if (siteId != null && decimal.Parse(siteId.ToString()) > 0)
                {
                    Util util = new Util();
                    _IsAuthorised = util.IsPermittedSite(decimal.Parse(siteId.ToString()));
                }
                if (!_IsAuthorised)
                {
                    filterContext.Controller.ViewBag.AuthorizationMessage = "You are not Authorised to access this resource; Please select a different site.";
                }
            }
            catch(Exception ex)
            {
                Logger.Error("Error in PermittedSiteActionAttribute>OnActionExecuting", ex);
            }
        }
    }
Here OnActionExecuting event I am checking the permission and if fails I am setting the Model to be Null at OnActionExecuted event - so that the user doesn't views the resource returned by the Action. May be not the perfect way of doing this - but a way around, I guess :)

To get started with these Asp .Net Action filters following tutorial is a good one:


Open source Push Server

  • PushSharp: [C#] :A server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), OSX (APNS 10.8+) Android (C2DM and GCM - Google Cloud Message), Chrome (GCM) Windows Phone, Windows 8, Blackberry (PAP), and Amazon (ADM) devices!
  • PushSharp.Web: [C#] A web wrapper for PushSharp
  • AeoroGear UnifiedPushSeerver: [JAVA]: Easy and promising - does have a Openshift gear and can be tested over Openshift
  • Pushd:[Node.js] :Pushd is a pluggable "unified" push server for server-side notification to mobile native apps, web apps etc. With pushd you can send push notifications to any supported mobile platform, web app or HTTP server from a single entry point. Pushd takes care of which device is subscribed to which event and is designed to support an unlimited amount of subscribable events.
  • Loopback.io: [Node.js]: Loopback is basically a nodejs framework to easily create api - but it does have component to create push servers.
  • Node-Push-Server: [Node.js]: It looks like a stripped down version of Pushd.

Thursday, May 21, 2015

Logging in DotNet applications

  • log4net: log4net from Apache is a library tool to help the programmer output log statements to
    a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the .NET runtime.
  • ELMAHELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.For asp.net applications to log without putting any code inside the applications

WCF vs Asp.Net Web API

When I first learnt about Web API, I got confused - as I was always doing the RESTful services using the WCF - why then another way to accomplish the same thing ?

WCF ASP.Net WEB API
  • Back-end Services
  • SOAP, WS-*
  • Transporst: HTTP, UDP, TCP, QUEUES, Custom
  • Message Patterns: request-response, one-way, duplex
  • Use WCF Web HTTP to add HTTP endpoints to existing WCF Services
  • Use WCF Data Services for full OData support
  • Front-end Services
  • Media Type: JSON, XML, form-URL-encoded, Custom
  • Request-reply only
  • REST, resource centric
  • Use SignalR for asynchronous signaling (polling, long-polling, WebSockets)

For further reference:

Friday, May 9, 2014

Opensource tools for Analyzing Website Traffic Stats

While developing reports fr one of my applications I have got some interest in data and analytic - while googling I have got some of the good works that are open sourced :) [There are always some good folks online :B] . I am listing couple of the tools here:


"Hummingbird lets you see how visitors are interacting with your website in real time."
Hummingbird is built on top of Node.js, and utilizes Websocket to provide 'Live' stats.
Platform : Node.js and javascript





Pikwik
"Piwik is the leading open source web analytics platform that gives you valuable insights into your website’s visitors, your marketing campaigns and much more, so you can optimize your strategy and online experience of your visitors."
Platform: PHP- MySql



For more of them check the list in the following link :

http://en.wikipedia.org/wiki/List_of_web_analytics_software

Monday, October 28, 2013

"WHERE IN" for LINQ

I was trying to convert the following query to LINQ :

select Osid,OsName,CompanyId,Pid,description,type from OrganisationStructure where OSId in (" + location + ") and type='location'
Where location is a string like '123,2345,12356' ...
What I got on some places the where in clause is converted as bellow :

List<string> locList = location.Split(',').ToList();
var orgloc = (from b in context.OrganisationStructures
                                  where locList.Contains(b.OSId.ToString()) select new{b.OSId,b.OSName,b.CompanyId,b.pID,b.description,b.Type});
This was not giving any error on design time. But while debugging I got the following error:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
The reason is LINQ to SQL doesn't know how to translate the .ToString() call to a SQL expression.Therefore I had to change it like bellow:
var orgloc = (from b in context.OrganisationStructures select new{b.OSId,b.OSName,b.CompanyId,b.pID,b.description,b.Type}).ToList().Where( s => locList.Contains(s.OSId.ToString()) && s.Type.Equals("location") ) ;

Refrence :

Wednesday, October 9, 2013

Opensource Chatbots/Chatterbots

I have started playing with a Program-O a chatbot that really worked for me. Liked it very much and while googling a little more I have found there is a good community who work for creating chatbots - thought I might list them so that I can check them later; Right now I am just listing and will update this post once I check each of them :

Some other good references I have got from Chatbots.org

Wednesday, September 11, 2013

Opensource applications with CodeIgniter

So far I have a crash for CodeIgnier and whenever I start developing for myself my first preference is always been CI :)
In this post I will be listing some of the Opensource projects built on top of CI :

Blogging tool :
Open blog: http://www.open-blog.info/
Stblog: https://github.com/stblog/Stblog >
Simple blogging App: https://github.com/indranil/scribe
Blog system App: https://github.com/mengu/ciblog
Blogging App: https://github.com/brenden/Ignited-Blog

CMS:
Ionize Cms: http://www.ionizecms.com/
Egypt CMS: http://blazeeboy.github.com/Codeigniter-Egypt/
Max CMS: http://max-3000.com/
Getfuel Cms: http://www.getfuelcms.com/
CodeFight Cms: http://codefight.org/
Pyro Cms: http://pyrocms.com/
Image CMS: https://github.com/firstrow/ImageCMS
Manager CMS: https://github.com/souzadavi/CMS
Photo gall CMS: https://github.com/bensonarts/GalleryCMS
Fotochest gall CMS: http://fotochest.com/

Forum
Forum App: http://sourceforge.net/projects/forumprojesi/
Dove Forum: https://github.com/ChrisBaines/Dove-Forums

Social Networking
Social Network Framework: https://github.com/valfreixo/Social-Network-Framework
Book Sharing social network: https://github.com/viaduk/BookSharing
Maran Cake Melody - Social Portal using Cakephp or CodeIgniter + Doctrine ORM : http://code.google.com/p/marancakemelody/

E-commerce
Social ecommerce platform: https://github.com/lammoth/Cloubiz

Other:
Classroom Bookings App: http://classroombookings.com/
Bamboo Invoice App: http://www.bambooinvoice.org/
MyClientBase Invoice App: http://myclientbase.com/
68 Knowledge Base App: https://github.com/68kb/
Sitemanager App: http://designelemental.net/sitemanagr
QuickSnaps Gall App: https://github.com/eoinmcg/QuickSnaps


Sunday, June 23, 2013

Dev boards to make your own custom computers !

The improvements of computers and the electronics has given (and possibly giving) birth of some evaluated couple of developer (bread !!)boards - They are more like some smart super-micro[bigger than nano :P] computers. Just wanted to go through some of the famous ones and list them as I go.
Somewhere I found that somebody was depicting the story as
Then Massimo Banzi invented a new single board: Arduino.
And everyone was happy. At last, prototyping was easy, cheap, and fast.
Then the unexpected happened: David Braben invented the Raspberry Pi. Everyone became happier. Prototyping became easier, cheaper, and faster.
The story does not end here: now we have CubieBoard, Gooseberry, APC Rock, OLinuXino,Hackberry A10, et caetera.
Here is a list of the boards I was studying to pick one up from :
 
Some fun stuff that you can do with Raspberry Pi
If you are a Windows user and don wanna wait to give it a look; check the follwoing link.
http://sourceforge.net/projects/rpiqemuwindows/. And here's a post how to start you DotNet[actually mono] programming on Raspberian

So far what my understanding is depending on the project you want to do/budget you have you have to carefully choose from the list. But for me I might go with Raspberrypi.
To find out a comprehensive comparison of the feature and price the following link can be checked.