Thursday, November 21, 2019

Custom authentication filter in mvc

Filters in asp.net MVC Tutrials

Filters in asp.net MVC


Filter is used to creating custom logic to execute before and after action methods.
we can apply the Filters in Controller level or Action method level.
Asp.net MVC provides different types of filters
  1. Authorization Filter
  2. Action Filter
  3. Result Filter
  4. Exception Filter
The Filter Execution sequence is shown below.

    Authentication Filters ==>  Authorization filter ==> Action filter ==> Result filter ==> Exceptionfilter
  1. Authentication Filter executes before any other filters like Authorization, result, etc.
  2. The Result filter executes before and after the execution of any action result
  3. The exception filter executes only if any action methods, filters, or results throws an exception in the entire application
  4. The action filter executes before and after any action method in the controller
  5. The authorization filter executes after Authentication filter and action method, or before any other filter like Authorization, result, etc

1. Authentication Filters  example syntax:

public class AuthenticationCustomFilter : ActionFilterAttribute, IAuthenticationFilter
{
   public void OnAuthentication(AuthenticationContext  filterContext)
   {
        //Write Your Code
   }
   public void OnAuthenticationChallenge(AuthenticationChallenge    Context filterContext)
   {
        //Write Your Code
   }
}

we can apply this Custome Filter in Controller Level and Action Method Level or Globally 

At Controller Level :

[AuthenticationCustomFilter]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

At Action Method Level :

public class HomeController : Controller
{
    [AuthenticationCustomFilter]
    public ActionResult Index()
    {
                return View();
    }
}

We can apply at Globally:
  1. If you apply at the global level then it will be applied to all the controller and action methods of an application.
  2. Go to Application_Start event of Global.asax.cs file 
  3. By default you can write FilterConfig.RegisterGlobalFilters() mehtod. 
example :

in Golbal.aspx.cs
public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
          //  AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
           // RouteConfig.RegisterRoutes(RouteTable.Routes);
            //BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

in App_Start Folder
// FilterConfig.cs located in App_Start folder 

  public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }

The [HandleError] filter Attribute is applied globally in MVC Application.
By default in every MVC application creates automatically.






No comments: