【问题标题】:IdentityServer4 role based authorization correct useIdentityServer4 基于角色的授权正确使用
【发布时间】:2021-04-18 18:12:07
【问题描述】:

我目前正在开发一个 .net5.0 IdentityServer4 应用程序。

我想使用基于角色的授权,不幸的是它似乎无法正常工作。

当我在控制器操作上使用[Authorization] 属性时,我可以正确登录。

当我在控制器操作中使用 [Authorize(Roles = "admin")] 属性时,我会被转发到拒绝访问页面 (403)。

当我使用我的 TestUser 和角色 admin 登录时,我会被转发到我的客户端应用程序上的拒绝访问页面。

我的应用程序结构如下所示:

  • IdentityServer4
  • OIDC客户端(文App,授权码+PKCE)

IdentityServer4 配置如下所示:

public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        new Client
        {
            ClientId = "oidcClient",
            ClientName = "Example Client Application",
            ClientSecrets = new List<Secret> { new Secret("secret".Sha256()) },

            AllowedGrantTypes = GrantTypes.Code,
            RedirectUris = new List<string> { "https://localhost:xxxx/signin-oidc" },
            AllowedScopes = new List<string>
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email,
                "role"
            },

            RequirePkce = true,
            AllowPlainTextPkce = false
        }
    };

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new[]
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Profile(),
        new IdentityResources.Email(),
        new IdentityResource
        {
            Name = "role",
            UserClaims = new List<string> { "role" }
        }
    };
}

public static List<TestUser> GetUsers()
{
    return new List<TestUser> {
        new TestUser {
            SubjectId = "123ABC",
            Username = "admin",
            Password = "password",
            Claims = new List<Claim> {
                new Claim(ClaimTypes.Email, "admin@test.com"),
                new Claim(ClaimTypes.Role, "admin")
            }
        }
    };
}

我的客户端应用上的 Oidc 连接配置如下所示:

services.AddAuthentication(options => 
{
    options.DefaultScheme = "cookie";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookie")
.AddOpenIdConnect("oidc", options => 
{
    options.Authority = "https://localhost:5000";
    options.ClientId = "oidcClient";
    options.ClientSecret = "secret";

    options.ResponseType = "code";
    options.UsePkce = true;
    options.ResponseMode = "query";

    // Edit 1
    options.TokenValidationParameters = new TokenValidationParameters
    {
       NameClaimType = ClaimTypes.Name,
       RoleClaimType = ClaimTypes.Role
    };

    options.SaveTokens = true;
});

我的 controller 操作非常简单 - 只有角色还没有策略 - 看起来像这样:

[Authorize(Roles = "admin")] // Roles does not work
//[Authorize] works fine
public IActionResult Home()
{
    return View();
}

感觉好像访问令牌没有传递给我的客户——只有身份令牌......

无论如何,当我调试用户声称的操作时,如下所示:

我使用 IdentityServer4 TestUser,但我还没有 IProfileService 实现。有必要吗?

你知道如何解决这个问题吗?

如何在我的应用程序中正确使用角色声明?

【问题讨论】:

  • 您能出示您的授权政策吗?
  • 感谢您的评论!我还没有使用策略——它是一个简单的 OIDC 客户端启动器。我只在控制器操作中使用 [Authorize(Roles = "admin")] 属性。

标签: c# identityserver4 openid-connect roles claims


【解决方案1】:

IdentityServer/OpenIDConnect 和 Microsoft 对名称声明的名称有不同的看法。所以你需要告诉你的应用你的角色和名称声明是什么。

像这样:

}).AddOpenIdConnect(options =>
{
    ...
    
    options.TokenValidationParameters = new TokenValidationParameters
    {
        NameClaimType = JwtClaimTypes.Name,
        RoleClaimType = JwtClaimTypes.Role,
    };

});

【讨论】:

  • 谢谢!为了独立于 IdentityModel NuGet,我将使用 ClaimTypes。不幸的是,我仍然有问题 - 我相应地更新了我的问题。那我还需要在我的 ClientResource 和 IdentityResource 和 ApiResource 中使用 ClaimTypes 吗?
  • 要调试此类问题,始终首先查看令牌内部,因此请确保在令牌中或从 userinfo 端点提供角色/名称声明。您需要将 UserClaims 添加到您希望在 ID 令牌中显示的 IdentityResource,并将 UserClaims 添加到您希望添加到访问令牌的 ApiResource 或 ApiScope。
  • 如果您接受我的回答,请随时将我的回答标记为已接受。
  • 使用 Fiddler 之类的工具来捕获原始 HTTP(s) 流量(不要忘记启用 HTTPS)getfiddler.com 然后找到以 ey.... 开头的令牌,然后将其粘贴进入jwt.io 解码令牌。
  • 资源中列出的 UserClaims 然后需要在 TestUser 下列出相同的声明。比如向用户添加新的 Claim(JwtClaimTypes.Role, "ceo")。
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 2019-05-27
  • 2022-01-01
相关资源
最近更新 更多