admin管理员组

文章数量:1123058

We want to use a custom authorization handler in ASP.NET Core 8. When debugging, I see that the handler's code gets hit and the authorization requirement succeeds, but then I get a 403, because the DenyAnonymousAuthorizationRequirement of the PassthroughAuthorizationHandler is not fulfilled.

But I am authenticated alright, I can see my name in the UI and everything else works fine, including the [Authorize] attribute with roles, which we use on other endpoints. It just fails with the custom handler.

The authentication is configured like this:

        services
            .AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
            .AddCertificate(CertificateAuthenticationDefaults.AuthenticationScheme,
              options => {
                  options.Events = new CertificateAuthenticationEvents {
                      OnCertificateValidated = context => {
                          var userManager = context.HttpContext.RequestServices.GetRequiredService<ICurrentUserManager>();
                          context.Principal = userManager.LoginUserWithCertificate(context
                                        .ClientCertificate);
                          context.Success();

                          return Task.CompletedTask;
                      }
                  };
              });
        
        services.AddAuthorization();
        services.AddScoped<IAuthorizationHandler, AppRolesAuthorizationHandler>();

And I made sure that in the Program.cs UseAuthentication() is called before UseAuthorization().

What am I missing here? How can I even find out where exactly it goes wrong?

本文标签: