我正在浏览ASP.NET MVC中的Controller类,并发现它实现了IAuthenticationFilter接口.但是我无法理解如何在我的控制器中实现它的方法OnAuthentication()和OnAuthenticationChallenge(),以及何时调用它们.
如果有人可以向我解释或与我分享任何解释这一点的链接,将会非常有帮助.即使我无法在MSDN中找到任何资源.
使用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)
| 归档时间: |
|
| 查看次数: |
5330 次 |
| 最近记录: |