我用ASP.NET Core2.0API、Identity Server和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")]
为什么我不在令牌中获取用户声明?