什么OverrideAuthenticationAttribute是为什么?
我在当前的Web API项目中遇到了一个标有System.Web.Http.OverrideAuthenticationAttribute的控制器方法,我很好奇这是什么?
在Google和Stackoverflow中搜索并不回答这个问题. MSDN documentation不包含太多信息.它只说如下:
另外,我已经看过来源: public sealed class OverrideAuthenticationAttribute : Attribute,IOverrideFilter,IFilter { public bool AllowMultiple { get { return false; } } public Type FiltersToOverride { get { return typeof(IAuthenticationFilter); } } } 但这并没有太多光线. 任何人都可以解释使用OverrideAuthenticationAttribute的目的是什么?请给出一些用例来更好的理解. 解决方法
OverrideAuthentication属性用于抑制全局认证过滤器,这意味着使用此过滤器时将禁用所有全局认证过滤器(实现IAuthenticationFilter).
假设您有一个名为BasicAuth的全局身份验证过滤器: public class BasicAuthAttribute : ActionFilterAttribute,IAuthenticationFilter { public void OnAuthentication(AuthenticationContext filterContext) { } public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext) { var user = filterContext.HttpContext.User; if (user == null || !user.Identity.IsAuthenticated) { filterContext.Result = new HttpUnauthorizedResult(); } } } 并且使用此代码将过滤器配置为所有控制器的全局过滤器: public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new BasicAuthAttribute()); } } 假设您想在单个控制器或控制器操作上使用其他身份验证策略.在这种情况下,您可以禁用全局身份验证.过滤器使用OverrideAuthentication属性,然后配置要用于该特定操作的新过滤器.当您与外部登录提供程序集成时,这是有帮助的,您不希望任何现有的全局身份验证过滤器弄乱您的外部登录身份验证. 在下面的代码中,禁用了全局身份验证筛选器,然后启用HostAuthentication筛选器以启用外部登录提供程序(例如Facebook)的单个操作: // GET api/Account/ExternalLogin [OverrideAuthentication] [HostAuthentication(Startup.ExternalCookieAuthenticationType)] [AllowAnonymous] [HttpGet("ExternalLogin",RouteName = "ExternalLogin")] public async Task<IHttpActionResult> ExternalLogin(string provider) { // Auth code } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – MVC授权 – 多个登录页面
- ASP.NET MVC:Application_Start和Url.Action
- asp.net – 从web.config中膨胀时,SmtpClient不会进行身份验
- asp.net-mvc-3 – 剃刀中的部分的默认内容
- IIS反向代理不使用ASP.NET中的Response.Redirect()
- asp.net-mvc – 为什么Microsoft堆栈说成本高昂?
- asp.net – MSDeploy.exe可以作为管理员连接,但不能连接任何
- asp.net – 当BackColor透明时,MS图表中的文字变得“块状”
- asp.net – 自定义主体在新请求时恢复为GenericPrincipal
- Asp.Net Core ModelState 验证总是false