2017-12-26 17 views
0

私の承認が成功しない理由を理解できません。カスタムAuthorizationHandler HandleRequirementAsyncが呼び出されない

https://github.com/aspnet/Security/issues/1103

は私の問題はさえベースの許可をリソースに関連していないがOPは、同様の問題があったように思える:

私はこの中には、潜在的な理由を検討していました。

は、ここに私のコードです:

AuthorizationHandler:

public class DebugOrDeveloperRequirementHandler : AuthorizationHandler<DebugOrDeveloperRequirement> 
{ 
    private readonly IHostingEnvironment _environment; 

    public DebugOrDeveloperRequirementHandler(IHostingEnvironment environment) 
    { 
     // breakpoint here - does get hit 
     _environment = environment; 
    } 

    /// <inheritdoc /> 
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, DebugOrDeveloperRequirement requirement) 
    { 
     // breakpoint here but never hit 
     if (_environment.IsDevelopment() || _environment.IsIntegrationTest() || context.User.IsInRole(Constants.RoleNames.Developer)) 
      context.Succeed(requirement); 

     return Task.CompletedTask; 
    } 
} 

要件:

public class DebugOrDeveloperRequirement : IAuthorizationRequirement 
{ 

} 

Startup.csコード:

 services.AddAuthorization(config => 
     { 
      config.AddPolicy(ApplicationPolicyNames.Contractor, builder => 
      { 
       builder.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) 
        .RequireAuthenticatedUser() 
        .RequireRole(DataLayer.Setup.Constants.RoleNames.Contractor, DataLayer.Setup.Constants.RoleNames.Developer, DataLayer.Setup.Constants.RoleNames.Admin); 
      }); 

      config.AddPolicy(ApplicationPolicyNames.Customer, builder => 
      { 
       builder.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) 
        .RequireAuthenticatedUser() 
        .RequireRole(DataLayer.Setup.Constants.RoleNames.Customer, DataLayer.Setup.Constants.RoleNames.Developer, DataLayer.Setup.Constants.RoleNames.Admin); 
      }); 

      config.AddPolicy(ApplicationPolicyNames.Administrator, builder => 
      { 
       builder.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) 
        .RequireAuthenticatedUser() 
        .RequireRole(DataLayer.Setup.Constants.RoleNames.Developer, DataLayer.Setup.Constants.RoleNames.Admin); 
      }); 

      config.AddPolicy(ApplicationPolicyNames.Developer, builder => 
      { 
       builder.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) 
        .RequireAuthenticatedUser() 
        .RequireRole(DataLayer.Setup.Constants.RoleNames.Developer); 
      }); 

      config.AddPolicy(ApplicationPolicyNames.DeveloperOrDebug, builder => 
      { 
       builder.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) 
        .Requirements.Add(new DebugOrDeveloperRequirement()); 
      }); 
     }); 
services.AddSingleton<IAuthorizationHandler, DebugOrDeveloperRequirementHandler>(); 

私のコードは、すべてのことが異なって見えるしません。前のmドキュメント。したがって、なぜこのAuthorizationHandlerが呼び出されないのかは分かりません。

答えて

0

さて、私は愚かな気がします - 私はアクションの認可属性がコントローラの属性を上書きすると思っていました。

私のコントローラーには開発者ポリシーがありました。そのコントローラーは、そのハンドラーが実行を開始する前にアクションが失敗するようにしました。

関連する問題