Sunday, September 17, 2017

Customized Authentication Filters in ASP.MVC5

As we studied in last article, Filters are used to inject logic at the different levels of request processing. Below is the filters execution sequence:
Authentication Filters ==>  Authorization filter ==> Action filter ==> Result filter ==> Exceptionfilter
  • The authentication filter executes before any other filter
  • The authorization filter executes after Authentication filter and action method, or before any other filter
  • The action filter executes before and after any action method
  • The result filter executes before and after the execution of any action result
  • The exception filter executes only if any action methods, filters, or results throws an exception
  • Create a new class
  • Implement IAuthenticationFilter Interface
  • Derive it from ActionFilterAttribute
  • Override the OnAuthentication method to run logic before the action methodg

  • OnAuthentication Method
  • The program invokes Authentication Filters by calling this method. This method creates the AuthenticationContext. AuthenticationContext has information about performing authentication. We can use this information to make authentication decisions based on the current context.

    OnAuthenticationChallenge Method

    This method executes after the OnAuthentication method. We can use the OnAuthenticationChallenge method to perform additional tasks on request. This method creates an AuthenticationChallengeContext the same way as OnAuthentication.
  • Opps Part 1 : Abstraction

      Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...