当前位置: 首页 > 编程语言 > C#

MVC5.0和新的IAuthenticationFilter分享

时间:2023-04-10 12:28:59 C#

MVC5.0和新的IAuthenticationFilter当我创建一个新的asp.netmvc4.0应用程序时,我做的第一件事是创建并设置自定义授权全局过滤器看起来像这样://FilterConfig.cspublicstaticvoidRegisterGlobalFilters(GlobalFilterCollectionfilters){//filters.Add(newHandleErrorAttribute());filters.Add(新的CustomAuthorizationAttribute());然后我像这样创建CustomAuthorizationAttribute://CustomAuthorizationAttribute.csprotectedoverridevoidHandleUnauthorizedRequest(AuthorizationContextfilterContext){if(filterContext.HttpContext.Request.IsAjaxRequest()){//处理AJAX请求filterContext.HttpContext.Response.StatusCode=403;filterContext.Result=newJsonResult{JsonRequestBehavior=JsonRequestBehavior.AllowGet};}else{//处理常规请求base.HandleUnauthorizedRequest(filterContext);//让FormsAuthentication根据web.config中定义的loginUrl进行重定向(如果有的话)}}我有两个控制器:HomeController和SecureControllerHomeC控制器装饰有[AllowAnonymous]属性。SecureController未使用[AllowAnonymous]属性修饰。HomeController的Index()ActionResult显示带有简单按钮的视图。当我单击按钮时,我对SecureController中的GetData()方法进行了ajax调用,如下所示:$("#btnButton").click(function(){$.ajax({url:'@Url.Action("GetData","Secure")',type:'get',data:{param:"test"},success:function(data,textStatus,xhr){console.log("SUCCESSGET");}});});不用说,当我单击按钮时,我触发了CustomAuthorizationAttribute,因为它是一个全局过滤器,而且还因为SecureController没有用[AllowAnonymous]属性修饰。好的,我已经完成了我的介绍......在asp.netmvc5.0中,我们现在引入了一个新的身份验证过滤器,它在授权过滤器之前被触发(这很好,我们可以获得更细粒度的控制未经身份验证的用户的确切方式区分(http401(http403)与经过IS身份验证但碰巧未授权的用户)。为了试用这个新的身份验证过滤器,我创建了一个新的asp.netmvc5.0(VSExpress2013forWeb)并开始执行以下操作:;filters.Add(新的CustomAuthenticationAttribute());//注意我使用的是身份验证而不是授权}Thenattribute:publicclassCustomAuthenticationAttribute:ActionFilterAttribute,IAuthenticationFilter{publicvoidOnAuthentication(AuthenticationContextfilterContext)(AuthenticationChallengeContextfilterContext){varuser=filterContext.HttpContext.User;if(user==null||!user.Identity.IsAuthenticated){filterContext.Result=newHttpUnauthorizedResult();我创建了一个HomeController。HomeController装饰有[AllowAnonymous]属性。在从VS2013启动应用程序之前,我在CustomAuthenticationAttribute的两种方法(OnAuthentication和OnAuthenticationChallenge)中设置了两个断点。当我启动应用程序时,我遇到了第一个断点(OnAuthentication)。然后,令我惊讶的是,我的HomeController的Index()ActionResult中的代码被执行了,而OnAuthenticationChallenge()方法的OnAuthenticationChallenge()仅在我返回View()后才执行。问:我有两个问题。问题1)我的印象是[AllowAnonymous]属性会自动绕过CustomAuthenticationAttribute中的任何代码,但我错了!我是否需要手动检查[AllowAnonymous]属性是否存在并跳过任何代码?问题2)为什么我的HomeControllerIndex()方法中的代码在OnAuthentication之后执行?刚刚意识到OnAuthenticationChallenge()中的代码在我返回View()后得到执行?我担心的是,如果用户未通过身份验证,我不希望执行Index()方法中的代码。也许我看错了。如果有人可以帮助我阐明这一点,那就太好了!此致Vince回复:问题1)我的印象是[AllowAnonymous]属性会自动绕过CustomAuthenticationAttribute中的任何代码,但我错了!我是否需要手动检查[AllowAnonymous]属性是否存在并跳过任何代码?据我所知,[AllowAnonymous]属性与CustomAuthenticationAttribute无关。他们有不同的目的。[AllowAnonymous]将在Authorization上下文中生效,但不会在Authentication上下文中生效。已实施身份验证过滤器以设置身份验证上下文。例如,AuthenticationContext为您提供执行身份验证的信息。您可以使用此信息根据当前上下文做出身份验证决策。例如,您可以根据身份验证上下文决定将ActionResult修改为不同的结果类型,或者您可以根据身份验证上下文决定更改当前主体,等等。OnAuthenticationChallenge方法在OnAuthentication方法之后运行。您可以使用OnAuthenticationChallenge方法对请求执行其他任务。Re:问题2)为什么我的HomeController的Index()方法中的代码在OnAuthentication之后执行?刚刚意识到OnAuthenticationChallenge()中的代码在我返回View()后得到执行?这是预期的行为。由于您拥有全局注册的身份验证过滤器,因此首先要做的是,在执行任何操作之前,它会首先触发OnAuthentication事件,就像您已经注意到的那样。然后执行索引的OnAuthenticationChallenge。一旦Action成功完成与Action关联的任何身份验证过滤器(即Index),OnAuthenticationChallenge就会运行,以便它可以为action结果做出贡献。正如您在OnAuthenticationChallenge的代码中所做的那样,您可以将ActionResult修改为HttpUnauthorizedResult,它将与ActionResult进行协商。在回答问题1时:[AllowAnnoymous]属性就像一个标志(它实际上并不实现逻辑)。它的存在仅在执行OnAuthorization期间由[Authorize]属性检查。反编译[Authorize]属性将揭示逻辑:boolskipAuthorization=filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute),inherit:true)||filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute),inherit:true);如果(skipAuthorization){返回;[AllowAnonymous]永远不会“自动”绕过自定义属性中的代码...所以问题1后半部分的答案是:是的-如果您希望自定义属性存在,那么您需要执行检查(类似于上面一个)用于自定义[Authorization]属性中的[AllowAnonymous]属性。我需要在这里澄清我的第二个问题:问题2)为什么我的HomeController的Index()方法中的代码在OnAuthentication之后执行?刚刚意识到OnAuthenticationChallenge()中的代码在我返回View()后得到执行?如果您想阻止用户在操作方法中执行代码,您实际上应该在OnAuthentication中测试凭据。OnAuthenticationChallenge是您使用自定义结果处理401的机会,例如将用户重定向到自定义控制器/操作并让他们有机会进行身份验证。公共类CustomAuthenticationAttribute:ActionFilterAttribute,IAuthenticationFilter{publicvoidOnAuthentication(AuthenticationContextfilterContext){varuser=filterContext.HttpContext.User;if(user==null||!user.Identity.IsAuthenticated){filterContext;ResultpResult=newHurtna(}}publicvoidOnAuthenticationChallenge(AuthenticationChallengeContextfilterContext){//修改filterContext.Result去某个特别的地方...如果你这样做//这里什么都没有,他们只会转到网站的默认登录名}}这是过滤器的更完整演练及其使用方法:http://jameschambers.com/2013/11/working-with-iauthenticationfilter-in-the-mvc-5-framework/干杯以上就是C#学习教程:MVC5.0和新的IAuthenticationFilter分享的全部内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多付出注意,本文收集自网络,不代表立场,如涉及侵权请点右联系管理员删除,如需转载请注明出处: