代码之家  ›  专栏  ›  技术社区  ›  Alexandra Damaschin

角色-Identity Server 4

  •  3
  • Alexandra Damaschin  · 技术社区  · 6 年前

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

    为什么我不在令牌中获取用户声明?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Alexandra Damaschin    6 年前

    对于感兴趣的人,我是如何修复的: 在配置文件中添加角色列表:

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