Sunday, April 26, 2020

Custom authentication filter in mvc

Filters in asp.net MVC Tutrials

Filters in asp.net MVC

The 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

    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

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

  • At Controller Level :[AuthenticationCustomFilter]
  • At Action Method Level: public class HomeController : Controller
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 the Application_Start event of Global.asax.cs file 
  3. By default, you can write FilterConfig.RegisterGlobalFilters() method. 

 in Global.aspx.cs got to App_Start Folder

 public class FilterConfig

     The [HandleError] filter Attribute is applied globally in MVC Application. The Filter Execution sequence is shown below.



public class AuthenticationCustomFilter : ActionFilterAttribute, IAuthenticationFilter
    {
        public void OnAuthentication(AuthenticationContext filterContext)
        {
            //Write Your Code Or Logic Here
        }

        public void OnAuthenticationChallenge(AuthenticationChallenge Context filterContext)
        {
            //Write Your Code

        }

    }

    public class HomeController : Controller

    {
        public ActionResult Index()

        {
            return View();
        }
    }
{

    [AuthenticationCustomFilter]
    public ActionResult Index()

    {
        return View();

    }

}



example :


public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //  AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            // RouteConfig.RegisterRoutes(RouteTable.Routes);
            //BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
// FilterConfig.cs located in App_Start folder
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

}



By default in every MVC application creates automatically.



No comments: