asp.net – Web API授权属性不在Action上工作
发布时间:2020-12-16 03:23:54 所属栏目:asp.Net 来源:网络整理
导读:我在我的WebAPI控制器操作上使用[Authorize]属性,它总是未经授权返回. 这是我的行动 [Authorize(Roles = "Admin")] public IQueryableCountry GetCountries() { return db.Countries; } 这是我在全局MessageHandler中设置授权的地方.这是为了测试我正在测试
我在我的WebAPI控制器操作上使用[Authorize]属性,它总是未经授权返回.
这是我的行动 [Authorize(Roles = "Admin")] public IQueryable<Country> GetCountries() { return db.Countries; } 这是我在全局MessageHandler中设置授权的地方.这是为了测试我正在测试用户. public class AuthenticationHandler1 : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request,CancellationToken cancellationToken) { if (!HttpContext.Current.User.Identity.IsAuthenticated) { HttpContext.Current.User = TestClaimsPrincipal(); } return base.SendAsync(request,cancellationToken); } private ClaimsPrincipal TestClaimsPrincipal() { var identity = new ClaimsIdentity(HttpContext.Current.User.Identity.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name,"some.user")); identity.AddClaim(new Claim(ClaimTypes.Role,"Admin")); identity.AddClaim(new Claim(ClaimTypes.Role,"Supervisor")); var testIdentity = new ClaimsIdentity(identity); var myPrincipal = new ClaimsPrincipal(testIdentity); return myPrincipal; } } 在Application_Start中的Global.asax.cs中注册 GlobalConfiguration.Configuration.MessageHandlers.Add(new MyProject.AuthenticationHandler1()); 它一直显示这个消息 {"Message":"Authorization has been denied for this request."} 解决方法
我制作了自定义授权属性,但它确实有效.
public class AuthorizationAttribute : System.Web.Http.AuthorizeAttribute { public string Roles { get; set; } protected override bool IsAuthorized(HttpActionContext actionContext) { ClaimsPrincipal currentPrincipal = HttpContext.Current.User as ClaimsPrincipal; if (currentPrincipal != null && CheckRoles(currentPrincipal)) { return true; } else { actionContext.Response = new HttpResponseMessage( System.Net.HttpStatusCode.Unauthorized) { ReasonPhrase = "Some message" }; return false; } } private bool CheckRoles(ClaimsPrincipal principal) { string[] roles = RolesSplit; if (roles.Length == 0) return true; return roles.Any(principal.IsInRole); } protected string[] RolesSplit { get { return SplitStrings(Roles); } } protected static string[] SplitStrings(string input) { if(string.IsNullOrWhiteSpace(input)) return new string[0]; var result = input.Split(',').Where(s=>!String.IsNullOrWhiteSpace(s.Trim())); return result.Select(s => s.Trim()).ToArray(); } } 像这样使用它 [AuthorizationAttribute(Roles = "SomeRole,Admin")] public IQueryable<Country> GetCountries() { } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – IIS 6上的ASP.NET MVC – 通配符映射 – 传
- asp.net-mvc – 设置Kendo UI Grid Popup(MVC)的宽度
- asp.net-mvc – 自动捆绑/缩小是否已在最终版本的MVC 4中获
- asp.net-mvc – asp.net mvc 4 – 谁调用_ViewStart.cshtml
- asp.net-mvc-4 – 如何为Outlook创建“互联网日历订阅”?
- asp.net-web-api – 使用自定义授权测试异步Web api方法
- B/S(Web)实时通讯解决方案
- asp.net-mvc-3 – ViewBag.Title错误
- Asp.net Mvc 6登录后立即获得用户声明
- asp.net-mvc – MVC;使用制表符时如何避免ID名称冲突?
推荐文章
站长推荐
- model-view-controller – 比较Dates DataAnnota
- asp.net – Oracle Managed Dataacess EF6自定义
- asp.net核心 – 在ASP.NET Core的WebDeploy期间,
- asp.net – 在FileUpload Control中获取文件的扩
- 在Managed Code通过Google Gmail发送邮件以及如何
- ASP.Net MVC 2中的多个环境
- asp.net-mvc – 如何最好地使用ASP.NET MVC中的E
- asp.net – 具有不同按钮类型的Asp按钮
- asp.net cookie,身份验证和会话超时
- ASP.net上的URL路由,需要从url获取参数
热点阅读