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: