gpt4 book ai didi

c# - 将 ASP.NET 角色授权与 IdentityServer3 隐式流结合使用

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:11 29 4
gpt4 key购买 nike

我的单页应用程序使用 OidcTokenManager 通过隐式流连接到 IdentityServer3 STS。客户端将 IDS3 访问 token 作为 Bearer Token 提供给 ASP.NET Core (WebApi) Web 服务; Web 服务应用程序配置为使用 IDS3 中间件并使用授权属性限制对其方法的访问。

SPA 客户端配置:

function configureTokenManager() {
console.log("configureTokenManager()");
var config = {
authority: $config.authority,
client_id: "BNRegistry",
redirect_uri: $config.webRoot + "/#/authorised/",
post_logout_redirect_uri: $config.webRoot + "/#/",

response_type: "id_token token",
scope: "openid profile email BNApi",

silent_redirect_uri: $config.webRoot + "/#/renew/",
silent_renew: true,

filter_protocol_claims: false
};
return new OidcTokenManager(config);
};

STS 中的范围配置:

new Scope
{
Name = "BNApi",
DisplayName = "BN Api",
Enabled = true,
Type = ScopeType.Resource,
Claims = new List<ScopeClaim>
{
new ScopeClaim(Constants.ClaimTypes.Name),
new ScopeClaim(Constants.ClaimTypes.Role)
}
}

WebApi 配置:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = Configuration["Authority"],
RequiredScopes = new[] {"BNApi"},
NameClaimType = IdentityModel.JwtClaimTypes.Name,
RoleClaimType = IdentityModel.JwtClaimTypes.Role
});

WebApi 方法:

[Authorize]
public IActionResult Get()
{
...
}

这按预期工作,使用 401 拒绝未经身份验证的用户。如果我在 api Controller 方法(例如 User.Claims.ToList())中检查用户的声明,它包含用户已分配到的任何角色的条目。

但是,如果我检查 User.Identity.Name 属性,它始终为 null,如果我查询 User.IsInRole("Administrator"),它始终为 false ,即使用户被分配到该角色。此外,如果我将角色名称添加到 Authorize 属性 ([Authorize(Role="Administrator")]),无论用户是否属于,都会被 401 拒绝到规定的角色。

如何让 IdentityServer3 与 ASP.NET 角色授权配合使用?

最佳答案

您是否尝试过重置 InboundClaimTypeMap

来自 IdentityServer3 文档页面 here :

When you inspect the claims on the about page, you will notice two things: some claims have odd long type names and there are more claims than you probably need in your application.

The long claim names come from Microsoft’s JWT handler trying to map some claim types to .NET's ClaimTypes class types.

不幸的是,此映射最终破坏了您定义为 name 的特定声明名称和 role ,因为他们的名字被转换了,不再映射到你所期望的。这导致 [Authorize(Roles = "")]User.IsInRole("")没有按预期工作。

在您的 API 中 Startup.cs您应该添加以下内容:

JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions()
{
...
});

编辑:以下信息不正确!。正如@Paul Taylor 所指出的,“AlwaysInclude 属性确保相关声明始终出现在身份 token 中(与客户端一起使用,而不是 API)。这是一个资源范围,因此该属性无效。 ”。感谢您帮助我更多地了解 IdentityServer 的工作原理:-)

<罢工>对于 NameRole访问API时声称包含,需要特别标记为alwaysInclude在你的ScopeClaim列表。

<罢工>
new Scope
{
Name = "BNApi",
DisplayName = "BN Api",
Enabled = true,
Type = ScopeType.Resource,
Claims = new List<ScopeClaim>
{
new ScopeClaim(Constants.ClaimTypes.Name, true), //<-- Add true here
new ScopeClaim(Constants.ClaimTypes.Role, true) // and here!
}
}

<罢工>

关于c# - 将 ASP.NET 角色授权与 IdentityServer3 隐式流结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850498/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com