I have a custom IAuthenticationFilter implementation registered in RegisterGlobalFilters()
. In my project I'm witnessing the following call sequence:
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?
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With