【问题标题】:Bearer was forbidden with Authorize filter in IdentityServer4IdentityServer4 中的授权过滤器禁止承载
【发布时间】:2016-08-20 07:37:17
【问题描述】:

在使用AspNetAuthorization 教程测试IdentityServer4 时,我添加了一个简单的[Authorize(Roles = "Administrator")],从那时起我得到了这个错误:

AuthenticationScheme: Bearer 被禁止。

我的用户有以下声明: new Claim(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 SO 帖子,但没有成功。

【问题讨论】:

    标签: c# authentication asp.net-core authorization identityserver4


    【解决方案1】:

    我终于有时间写下角色检查在索赔世界中的工作原理:

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

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

    【讨论】:

    • 感谢您的回答。人们不能使用编写良好的 IdS 尤其是 IdS4 的原因之一是缺乏大量示例和文档来解决他们的问题。我被这个问题困扰了好几天。感谢您的广泛解释。
    • 这个答案为我指明了正确的方向。就我而言,我在 Postman 中进行测试,但忘记正确请求范围和 response_type 以获取包含所述信息的令牌。
    【解决方案2】:

    事实上,在阅读@leastprivilege 详细答案之前,我已经解决了我的问题。

    问题在于声明类型的命名,

    我更改了以下内容:

    new Claim(ClaimTypes.Role, "Administrator");
    

    到这里:

    new Claim(JwtClaimTypes.Role, "Administrator");
    

    授权成功了。那是因为它们之间的底层字符串值不同,而我的配置期望的是“角色”:

    ClaimTypes.Role => "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
    JwtClaimTypes.Role => "role"
    

    或者可以根据他的回答来做到这一点:

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

    背后的详细原因,请阅读@leastprivilege 回答

    【讨论】:

      【解决方案3】:

      根据附加资源,您实际上应该将策略名称放在授权属性中,就像[Authorize("AdministratorOnly")]

      https://damienbod.com/2016/02/14/authorization-policies-and-data-protection-with-identityserver4-in-asp-net-core/

      【讨论】:

      • 我已经通过[Authorize(Policy = "AdministratorOnly")] 应用了它,但它没有用。
      • @MohsenAfshin 我注意到您在上面定义的声明中没有发行人new Claim(ClaimTypes.Role, "Administrator", ClaimValueTypes.String, Issuer)。也许这是你的问题?基于示例github.com/blowdart/… 中发布的声明
      • 试过了,设置了issuer,好像config有问题,找不到。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 2016-02-23
      • 2017-10-10
      • 2020-11-29
      • 2015-02-22
      • 2011-08-05
      • 1970-01-01
      相关资源
      最近更新 更多