角色 - Identity Server 4

3

我有一个用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")]

为什么我的令牌中没有用户声明?
1个回答

6
如果您感兴趣,这是我如何解决它的方法: 在您的配置文件中添加一个角色列表:
new ApiResource
 (
   "fiver_auth_api", 
   "Fiver.Security.AuthServer.Api", 
    new List<string> {"role"} <--- Add this line to your API
  )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接