Filter in MVC

What is filters in Mvc??

In this article we understand about filters in MVC. Let's understand sometimes we need to perform logic either before an action method is called or after an action methods runs.
This above scenario to support Asp.net MVC provides filters.

There are following types in filters:
  1. Authentication Filters
  2. Authorization filters
  3. Action Filters
  4. Result filters
  5. Exception filters
  6. Output Caching filters
  1. Authentication Filters

Let's understand What is authentication filter. Authentication filter is a new feature of Asp.net MVC. This filter executes before any other filter.

Actually ASP.NET MVC does not provide any built-in authentication filter(s). However it provides you with the framework, so you can easily create your own custom authentication filters.

So when you create an Authentication filter you must implement the IAuthencationFilter. So Let's understand below implementation of Asp.net MVC IAuthentication.



public interface IAuthenticationFilter

{

    void OnAuthentication(AuthenticationContext filterContext);

    void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
}

So this Interface is used for call CustomAuthentication derived class. So let's see below:

public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter

{    public void OnAuthentication(AuthenticationContext filterContext)  

  {     }

    public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
    {       }


}

That means this two methods are required for implementation to CustomAuthenticationAttribute .


  • OnAuthentication
  • OnAuthenticationChallenge


Note: Actions are public methods in a controller. Action filters are attributes,that can be applied either on a controller or on a controller action method, which allow us to add pres and post processing logic to the action methods.

So,In simple terms an action filter allow us to executes some custom code,either,just before an action method is executed or immediately after an action method completes execution.

The custom action filter that we are going to build, should log the following information to a text file.

The name of the controller
The name of the action method
Execution time
If there is an exception, log the exception message and the time of the exception

   2. Authorization filters
In Asp.net MVC , by default all the controller action methods are accessible to both anonymous and authenticated users. If you want to action methods, to be available only for authenticated and authorized users, Then use Authorize filter .
Authorization Filter  will be executed once after user is authenticated.
In this step let's create a custom Authorization filter. For this create a class which inherits AuthorizeAttribute or implements IAuthorizationFilter interface. So this understand  we are going here is just passing a message to View.

public class CustomAuthFilter : AuthorizeAttribute
    {
public override void OnAuthorization(AuthorizationContext filterContext)
 {            
  filterContext.Controller.ViewBag.AutherizationMessage = "Custom Authorization: this Message from OnAuthorization method ";
  }        
    }



Post a Comment

0 Comments