asp.net-mvc – 实现MVC 5 IAuthenticationFilter
执行OnAuthentication和OnAuthenticationChallenge之前,OnAuthentication和OnAuthenticationChallenge除了在执行操作之前运行,OnAuthenticationChallenge在执行操作之后但在处理动作结果之前运行.
看来,他们中的任何一个(OnAuthentication或OnAuthenticationChallenge)都可以执行身份验证所需的所有操作.为什么需要2种方法? 我的理解是OnAuthentication是我们将逻辑逻辑(或者这个逻辑应该在实际操作方法中),连接到数据存储并检查用户帐户. OnAuthenticationChallenge是我们重定向到登录页面的地方,如果不通过验证.它是否正确?为什么我不能重定向OnAuthentication并且不实现OnAuthenticationChallenge.我知道有一些我失踪的东西有人可以向我解释吗? 存储已验证用户的最佳做法是什么,以便后续请求不必连接到db再次检查用户? 请记住,我是ASP.NET MVC的新手. 解决方法
这些方法真的是为了不同的目的:
> IAuthenticationFilter.OnAuthentication应用于设置主体,主体是标识用户的对象. 您还可以像HttpUnauthorisedResult一样在此方法中设置结果(这将使您无法执行其他授权筛选器).虽然这是可能的,我喜欢分离不同的过滤器之间的关注. >这个结果总是在结果返回给用户之前执行,这意味着它可以在不同请求的管道的不同点执行.请参阅下面的ControllerActionInvoker.InvokeAction的说明. > IAuthorizationFilter.OnAuthorization还应用于执行身份验证检查,例如检查用户是否登录或属于某个角色. 如果您检查 > IAuthenticationFilter.OnAuthentication为每个身份验证过滤器调用.如果在AuthenticationContext中更新了主体,则更新context.HttpContext.User和Thread.CurrentPrincipal. 我已经将ControllerActionInvoker.InvokeAction的当前代码复制为参考,但可以使用上面的链接查看最新版本: public virtual bool InvokeAction(ControllerContext controllerContext,string actionName) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } Contract.Assert(controllerContext.RouteData != null); if (String.IsNullOrEmpty(actionName) && !controllerContext.RouteData.HasDirectRouteMatch()) { throw new ArgumentException(MvcResources.Common_NullOrEmpty,"actionName"); } ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext); ActionDescriptor actionDescriptor = FindAction(controllerContext,controllerDescriptor,actionName); if (actionDescriptor != null) { FilterInfo filterInfo = GetFilters(controllerContext,actionDescriptor); try { AuthenticationContext authenticationContext = InvokeAuthenticationFilters(controllerContext,filterInfo.AuthenticationFilters,actionDescriptor); if (authenticationContext.Result != null) { // An authentication filter signaled that we should short-circuit the request. Let all // authentication filters contribute to an action result (to combine authentication // challenges). Then,run this action result. AuthenticationChallengeContext challengeContext = InvokeAuthenticationFiltersChallenge( controllerContext,actionDescriptor,authenticationContext.Result); InvokeActionResult(controllerContext,challengeContext.Result ?? authenticationContext.Result); } else { AuthorizationContext authorizationContext = InvokeAuthorizationFilters(controllerContext,filterInfo.AuthorizationFilters,actionDescriptor); if (authorizationContext.Result != null) { // An authorization filter signaled that we should short-circuit the request. Let all // authentication filters contribute to an action result (to combine authentication // challenges). Then,run this action result. AuthenticationChallengeContext challengeContext = InvokeAuthenticationFiltersChallenge( controllerContext,authorizationContext.Result); InvokeActionResult(controllerContext,challengeContext.Result ?? authorizationContext.Result); } else { if (controllerContext.Controller.ValidateRequest) { ValidateRequest(controllerContext); } IDictionary<string,object> parameters = GetParameterValues(controllerContext,actionDescriptor); ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext,filterInfo.ActionFilters,parameters); // The action succeeded. Let all authentication filters contribute to an action result (to // combine authentication challenges; some authentication filters need to do negotiation // even on a successful result). Then,postActionContext.Result); InvokeActionResultWithFilters(controllerContext,filterInfo.ResultFilters,challengeContext.Result ?? postActionContext.Result); } } } catch (ThreadAbortException) { // This type of exception occurs as a result of Response.Redirect(),but we special-case so that // the filters don't see this as an error. throw; } catch (Exception ex) { // something blew up,so execute the exception filters ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext,filterInfo.ExceptionFilters,ex); if (!exceptionContext.ExceptionHandled) { throw; } InvokeActionResult(controllerContext,exceptionContext.Result); } return true; } // notify controller that no method matched return false; } 至于在设置主体时,在每次请求时都不会碰到数据库,可以使用某种服务器端缓存. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 无法从代码隐藏中调用App_Code类
- asp.net – recaptcha https问题https://api-secure.recapt
- asp.net-mvc – 如何获取某些代码在ASP.NET MVC 2中的每个单
- asp.net-mvc-3 – 如何关闭我的整个ASP.NET MVC 3网站的缓存
- asp.net – 从NCache Server获取所有密钥
- asp.net-mvc – 我可以使用Visual Studio的快速版本制作ASP
- asp.net-core – 项目’XXXXXX’没有锁文件.请运行“dotnet
- asp.net – 如何绑定gridview中数据表的复选框值
- asp.net-mvc – ASP .Net MVC 3:子动作和重定向
- asp.net-core – 简化的IOptions方法
- asp.net-mvc – ASP.NET MVC2 – 自定义模型绑定
- asp.net-mvc – Asp.net MVC控制器Action字符串参
- asp.net-web-api – ASP.NET WebApi自定义序列化
- asp.net – sessionState超时不适用于DefaultSes
- 我如何在ASP.Net Web窗体中模拟/伪造会话对象?
- asp.net 读取xml文件里面的内容,绑定到dropdown
- ASP.NET 2.0到4.0
- asp.net-mvc – asp.net MVC Url.Content()CDN重
- asp.net-mvc – Web路由如何工作?
- ASP.NET MVC – 将外部模型传递给编辑器模板