我有一个用Asp.Net Core 2.0 API、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")]
为什么我的令牌中没有用户声明?