我已经使用OWIN中间件实现了基于令牌的身份验证Web Api 2应用程序,成功进行了身份验证,我可以在其中检索令牌并使用它来获取Web Api的方法 .
但是,当我尝试添加角色授权时,它不起作用,我已经彻底搜索并发现我必须在oAuthorization提供程序中添加以下“GrantResourceOwnerCredentials”:
identity.AddClaim(new Claim(ClaimTypes.Role, "the role that i need to add"));
以上这一行是我能得到的,它也可以在Authorization roles WebAPI oauth owin找到
但是,每当我使用令牌获取任何方法(即使是具有不同角色的授权方法)时,它仍然会正常检索结果 .
我的意思是,例如在API控制器中:它类似于以下内容:
[Authorize(Roles = "Admin")]
// GET api/Patient
public IQueryable<Patient> GetPatients()
而在“GrantResourceOwnerCredentials”方法中,我只添加了一个Employee角色:
var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, "Employee"));
此外,不更新包含角色“AspNetUserRoles”的服务器资源管理器中的表 .
我错过了什么?