IAuthenticationFilter interface implementation in asp.net MVC
asp.net-mvc
Solution
Use `OnAuthentication` for setting or modifying the principal for the current request.
Use `OnAuthenticationChallenge` for validating current principal and permitting the execution of current request. For example:
public class CustomAuthenticatioFilter : ActionFilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
//Here you are setting current principal
filterContext.Principal = new ClaimsPrincipal();
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
//Here you're checking current action and redirecting to ErrorPage
filterContext.Result = new RedirectToRouteResult("ErrorPage",null);
}
}
Problem
I was going through the Controller class in ASP.NET MVC, and found out that it implements IAuthenticationFilter interface. However I am unable to understand how can I implement, its methods OnAuthentication() and OnAuthenticationChallenge(), in my controller and when these will be called. It will be very helpful if someone can explain it to me or share with me any link that explains this. Even I was unable to find any resource on this in MSDN.