默认的挑战方案在RouteGroups中无法工作?

huangapple go评论106阅读模式
英文:

Default challenge scheme isn't working with RouteGroups?

问题

我有一个用于多个端点的路由组:

  1. internal class EmployeeRouteGroup : RouteGroup
  2. {
  3. /// <inheritdoc />
  4. protected override string Prefix => "employees";
  5. /// <inheritdoc />
  6. protected override RouteGroupBuilder ConfigureCommonBehaviour(RouteGroupBuilder routeGroupBuilder)
  7. {
  8. return routeGroupBuilder.RequireAuthorization();
  9. }
  10. /// <inheritdoc />
  11. protected override RouteGroupBuilder ConfigureRoutes(RouteGroupBuilder routeGroupBuilder)
  12. {
  13. routeGroupBuilder.MapGet("/", async ([AsParameters] SearchEmployeesParametersDto getEmployeesParameters, IMediator mediator, CancellationToken cancellationToken)
  14. => await mediator.Send(new SearchEmployeesQuery { Parameters = getEmployeesParameters }, cancellationToken));
  15. routeGroupBuilder.MapGet("/{id}", async (int id, IMediator mediator, CancellationToken cancellationToken)
  16. => await mediator.Send(new GetEmployeeDetailsQuery { EmployeeId = id }, cancellationToken));
  17. routeGroupBuilder.MapGet("/for-salary-access-user/{id}", async (int id, IMediator mediator, CancellationToken cancellationToken)
  18. => await mediator.Send(new SearchEmployeesForSalaryAccessUserQuery { SalaryAccessUserId = id }, cancellationToken));
  19. return routeGroupBuilder;
  20. }
  21. }

还在 Startup.cs 中设置了 DefaultChallengeScheme

  1. services.AddAuthentication(options =>
  2. {
  3. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  4. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  5. options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
  6. }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o => o = jwtOptions);

但每次尝试进行未授权的请求时,都会收到 404 响应而不是 401

如果在端点内部放置 [Authorize(AuthenticationScheme = ...)] 则可以正常工作!但是,您知道,这不是一个好方法 默认的挑战方案在RouteGroups中无法工作?

英文:

I have a Route Group for several endpoints:

  1. internal class EmployeeRouteGroup : RouteGroup
  2. {
  3. /// &lt;inheritdoc /&gt;
  4. protected override string Prefix =&gt; &quot;employees&quot;;
  5. /// &lt;inheritdoc /&gt;
  6. protected override RouteGroupBuilder ConfigureCommonBehaviour(RouteGroupBuilder routeGroupBuilder)
  7. {
  8. return routeGroupBuilder.RequireAuthorization();
  9. }
  10. /// &lt;inheritdoc /&gt;
  11. protected override RouteGroupBuilder ConfigureRoutes(RouteGroupBuilder routeGroupBuilder)
  12. {
  13. routeGroupBuilder.MapGet(&quot;/&quot;, async ([AsParameters] SearchEmployeesParametersDto getEmployeesParameters, IMediator mediator, CancellationToken cancellationToken)
  14. =&gt; await mediator.Send(new SearchEmployeesQuery { Parameters = getEmployeesParameters }, cancellationToken));
  15. routeGroupBuilder.MapGet(&quot;/{id}&quot;, async (int id, IMediator mediator, CancellationToken cancellationToken)
  16. =&gt; await mediator.Send(new GetEmployeeDetailsQuery { EmployeeId = id }, cancellationToken));
  17. routeGroupBuilder.MapGet(&quot;/for-salary-access-user/{id}&quot;, async (int id, IMediator mediator, CancellationToken cancellationToken)
  18. =&gt; await mediator.Send(new SearchEmployeesForSalaryAccessUserQuery { SalaryAccessUserId = id }, cancellationToken));
  19. return routeGroupBuilder;
  20. }
  21. }

Also in Startup.cs I've setup DefaultChallengeScheme:

  1. services.AddAuthentication(options =&gt;
  2. {
  3. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  4. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  5. options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
  6. }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =&gt; o = jwtOptions);

But everytime when I'm trying to make unauthorized request I get 404 response instead of 401.

If you put [Authorize(AuthenticationScheme = ...)] within endpoint it works fine! But, you know, it's not good approach 默认的挑战方案在RouteGroups中无法工作?

答案1

得分: 1

问题实际上是在 Authentication 设置之后存在一个 Identity 设置。

AddIdentity&lt;&gt; 方法包含以下代码:

  1. services.AddAuthentication(options =>
  2. {
  3. options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
  4. options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
  5. options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
  6. })
  7. .AddCookie(IdentityConstants.ApplicationScheme, o =>
  8. {
  9. o.LoginPath = new PathString("/Account/Login");
  10. o.Events = new CookieAuthenticationEvents
  11. {
  12. OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
  13. };
  14. })

因此,要解决这个问题,您只需要在设置 Identity 之后设置您的身份验证。

英文:

The problem was in fact that there was an Identity setup after Authentication setup.

AddIdentity&lt;&gt; method contains this code:

  1. services.AddAuthentication(options =&gt;
  2. {
  3. options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
  4. options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
  5. options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
  6. })
  7. .AddCookie(IdentityConstants.ApplicationScheme, o =&gt;
  8. {
  9. o.LoginPath = new PathString(&quot;/Account/Login&quot;);
  10. o.Events = new CookieAuthenticationEvents
  11. {
  12. OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
  13. };
  14. })

So, to solve this issue you have to just setup your Authentication after Identity.

huangapple
  • 本文由 发表于 2023年7月20日 13:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定