asp.net MVC中的IAuthenticationFilter接口实现

Bib*_*bhu 3 asp.net-mvc

我正在浏览ASP.NET MVC中的Controller类,并发现它实现了IAuthenticationFilter接口.但是我无法理解如何在我的控制器中实现它的方法OnAuthentication()和OnAuthenticationChallenge(),以及何时调用它们.

如果有人可以向我解释或与我分享任何解释这一点的链接,将会非常有帮助.即使我无法在MSDN中找到任何资源.

Ole*_*Aza 7

使用OnAuthentication用于设置或修改当前请求的主体.

使用OnAuthenticationChallenge用于验证当前主体并允许当前请求的执行.例如:

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);
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果只有`IAuthenticationFilter`的MSDN文档包含你的前两句话! (2认同)