Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of IAuthenticationFilter.OnAuthenticationChallenge()

I have a custom IAuthenticationFilter implementation registered in RegisterGlobalFilters(). In my project I'm witnessing the following call sequence:

  1. IAuthenticationFilter.OnAuthentication
  2. authorization (if any)
  3. controller action
  4. IAuthenticationFilter.OnAuthenticationChallenge

Why does it happen after controller action? From this blog post I read

The key thing to remember is that OnAuthenticationChallenge does not necessarily run before every other Action Filter. It can run at various stages.

How can it be useful if we can't tell when exactly it is called?

like image 872
UserControl Avatar asked Mar 06 '14 11:03

UserControl


People also ask

How do I use IAuthenticationFilter?

This IAuthenticationFilter interface has 2 methods. Open Visual Studio 2015 or an editor of your choice and create a new project. Choose the "web application" project and give an appropriate name to your project. Select the "empty" template, check on the MVC box and click OK.

Does ASP NET identity use IAuthenticationFilter?

You probably know that the ASP.NET MVC Controller itself is also an ActionFilter. The new IAuthenticationFilter provides a great ability to customize authentication within an ASP.NET MVC 5 application.

Why filters are used in MVC?

ASP.NET MVC Filters are used to inject extra logic at the different levels of MVC Framework request processing. Filters provide a way for cross cutting concern (logging, authorization, and caching).


1 Answers

Source

"The OnAuthenticationChallange method is called by the MVC Framework whenever a request has failed the authentication or authorization policies for an action method. The OnAuthenticationChallenge method is passed an AuthenticationChallengeContext object, which is derived from the ControllerContext class"

So, a practical example would be:

1 - You set your custom authorize filter

2 - users fails on authorizing method

3 - OnAuthenticationChallenge method is called.

like image 194
DmitryK Avatar answered Nov 03 '22 01:11

DmitryK