gpt4 book ai didi

c# - Bearer 被 IdentityServer4 中的授权过滤器禁止

转载 作者:行者123 更新时间:2023-11-30 13:36:07 28 4
gpt4 key购买 nike

测试时IdentityServer4AspNetAuthorization 教程我添加了一个简单的 [Authorize(Roles = "Administrator")] 然后我得到了这个错误:

AuthenticationScheme: Bearer was forbidden.

我的用户有这样的声明:新声明(ClaimTypes.Role, "Administrator", ClaimValueTypes.String)

ConfigureServices 方法中:

 services.AddAuthorization(options =>
{
options.AddPolicy("AdministratorOnly", policy => policy.RequireRole("Administrator"));
});

services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();

config.Filters.Add(new AuthorizeFilter(policy));
});

Configure 方法中:

   app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
{
Authority = "http://localhost:5000",
ScopeName = "openid",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
});

调试输出:

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Debug: Executing action LearningEntityServer4.OAuth.ValuesController.Get (LearningEntityServer4.OAuth)
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization was successful for user: myuser.
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization failed for user: myuser.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Warning: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult: Information: Executing ChallengeResult with authentication schemes ().
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware: Information: AuthenticationScheme: Bearer was forbidden.

我在配置中遗漏了什么?

PS:我已经查过了this所以发布没有成功。

最佳答案

我终于抽出时间写下声明世界中角色检查如何工作的内部机制:

https://leastprivilege.com/2016/08/21/why-does-my-authorize-attribute-not-work/

简而言之 - 确保您用于角色的声明类型与您的 ClaimsIdentity 上的 RoleClaimType 相匹配。或者在您的策略中将 RequireRole 替换为 RequireClaim 并使用正确的类型。

关于c# - Bearer 被 IdentityServer4 中的授权过滤器禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39051705/

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