角色-Identity Server 4

亚历山德拉(Alexandra Damaschin)

我有一个使用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, "[email protected]"),
                    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")]

为什么我没有获得用户要求的令牌?

亚历山德拉(Alexandra Damaschin)

对于感兴趣的人,我可以通过以下方法解决它:在您的配置文件中,为您的角色添加一个列表:

new ApiResource
 (
   "fiver_auth_api", 
   "Fiver.Security.AuthServer.Api", 
    new List<string> {"role"} <--- Add this line to your API
  )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章