我有一个项目完成了Asp.Net核心2.0API,身份服务器和WPF应用程序。登录后,我可以从WPF访问API。
现在我正在尝试实现角色,这样我就可以授权某些用户访问API。
在Config.cs中,我声明我的客户机并添加到范围中:
new Client
{
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.OfflineAccess,
"fiver_auth_api",
"role"
},
AlwaysIncludeUserClaimsInIdToken=true
}声明TestUser:
return new List<TestUser>
{
new TestUser
{
SubjectId = "", Username = "", Password = "",
Claims = new List<Claim>
{
new Claim(JwtClaimTypes.Email, "AliceSmith@email.com"),
new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
new Claim(JwtClaimTypes.Role, "Admin"),
new Claim(JwtClaimTypes.Scope, "openid offline_access fiver_auth_api")
}
}
} 在我使用的控制器中:
[Authorize(Roles = "Admin")]为什么我没有在令牌中得到用户声明?
发布于 2018-05-25 13:09:32
对于感兴趣的人,我是如何修复的:在配置文件中为您的角色添加一个列表:
new ApiResource
(
"fiver_auth_api",
"Fiver.Security.AuthServer.Api",
new List<string> {"role"} <--- Add this line to your API
)https://stackoverflow.com/questions/50525347
复制相似问题