大佬教程收集整理的这篇文章主要介绍了asp.net – 如何在没有查找AspNetUserRoles表的情况下获取WebAPI方法中的用户角色?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用具有承载令牌的认证和ASP.NET Identity 2.1的WebApi,应用程序始终在浏览器中运行。我的用户已经设置了相应的角色。
我放置一些代码来获取userId,然后转到AspNetUserRoles表,以获取方法开始时的角色。但是我注意到这需要大约500毫秒才能运行。另外我正在考虑以下几点:
[httpPut] [Authorize(Roles = "Admin")] [Route("AdminupdateStatus/{userTestId:int}/{userTestStatusId:int}")] public async Task<IhttpActionResult> AdminupdateStatus(int userTestId,int userTestStatusId) { return await updateStatusMethod(userTestId,userTestStatusId,"Admin"); } [httpPut] [Authorize(Roles = "student")] [Route("studentupdateStatus/{userTestId:int}/{userTestStatusId:int}")] public async Task<IhttpActionResult> studentupdateStatus(int userTestId,"student"); } [httpPut] [Authorize(Roles = "Teacher")] [Route("TeacherupdateStatus/{userTestId:int}/{userTestStatusId:int}")] public async Task<IhttpActionResult> TeacherupdateStatus(int userTestId,"Teacher"); } private async Task<IhttpActionResult> updateStatusMethod(int userTestId,int userTestStatusId,String roleName) { // Call the stored procedure here and pass in the rolename }
这是一个有效的方法来做到这一点,或者还有另一种更干净的方法。我不太清楚的是,如果前端或后端缓存用户角色。我认为这是完成或有一些设置将允许这样做。
注意我正在使用声明将角色信息发送给我的客户端:
public static AuthenticationProperties CreateProperties( String userName,ClaimsIdentity oAuthIdentity,String firstName,String lastName,int organization) { IDictionary<String,String> data = new Dictionary<String,String> { { "userName",userNamE},{ "firstName",firstNamE},{ "lastName",lastNamE},{ "organization",organization.ToString()},{ "roles",String.Join(":",oAuthIdentity.Claims.Where(c=> c.Type == ClaimTypes.RolE).SELEct(c => c.value).ToArray())} }; return new AuthenticationProperties(data); }
但是,这里的问题与服务器有关,如果用户在不去数据库的情况下,我可以检查我的方法。也许有一种方法可以安全地使用声明,但我不知道如何做到这一点。
任何帮助和建议将不胜感激。
我的建议是从数据库中读取用户的角色/声明,您无需为此尝试解决此问题,所有您需要做的是在用户登录方法GrantresourceownerCredentials时为用户设置声明您可以这样设置,让用户从DB读取角色并将其设置为“Role”类型的声明
var identity = new ClaimsIdentity(context.options.AuthenticationTypE); identity.AddClaim(new Claim(ClaimTypes.Name,context.UserName)); identity.AddClaim(new Claim(ClaimTypes.Role,"Admin")); identity.AddClaim(new Claim(ClaimTypes.Role,"Supervisor"));
记住,只有当用户登录时才会发生一次,那么您将收到包含所有这个用户声明的承载签名和加密令牌,不需要任何数据库访问来进行验证。
或者如果要从数据库创建身份,您可以使用以下内容:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UseRMANager<ApplicationUser> manager,String authenticationTypE) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreatEIDentityAsync(this,authenticationTypE); // Add custom user claims here return userIdentity; }
然后在GrantresourceownerCredentials中执行以下操作:
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(useRMANager,OAuthDefaults.AuthenticationTypE);
现在,一旦将承载令牌发送到具有Authorize(Authorize(Roles =“Teacher”)]的Authorize属性的受保护终端,我可以向您保证,您的代码将不会访问DB以执行任何查询,打开SQL分析器并检查它将从加密的令牌中读取声明以及角色声明,并检查此用户是否属于教师角色,并允许或拒绝请求。
我已经发布了一系列关于Token Based Authentication的5个帖子以及Authorization server和JWT tokens.我建议你阅读这些帖子来更好地了解持卡人的记号。
以上是大佬教程为你收集整理的asp.net – 如何在没有查找AspNetUserRoles表的情况下获取WebAPI方法中的用户角色?全部内容,希望文章能够帮你解决asp.net – 如何在没有查找AspNetUserRoles表的情况下获取WebAPI方法中的用户角色?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。