gpt4 book ai didi

角色 - Identity Server 4

转载 作者:行者123 更新时间:2023-12-01 10:34:38 25 4
gpt4 key购买 nike

我有一个使用 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
}

声明测试用户:

  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")
}
}
}

在我使用的 Controller 中:

[Authorize(Roles = "Admin")]

为什么我没有在 token 中获取用户声明?

最佳答案

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

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

关于角色 - Identity Server 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50525347/

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