在IdentityServer4中禁止使用授权过滤器

时间:2016-08-20 07:37:18

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

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

  

AuthenticationScheme:禁止持票人。

我的用户有此声明: 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帖子没有成功。

3 个答案:

答案 0 :(得分:8)

我终于找到时间写下角色检查在声明世界中如何运作的内部结构:

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

事实上,我在阅读@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回答