Skip to content

Setting DefaultScheme in authentication configuration is not working #46232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
glen-84 opened this issue Jan 24, 2023 · 6 comments
Closed
1 task done

Setting DefaultScheme in authentication configuration is not working #46232

glen-84 opened this issue Jan 24, 2023 · 6 comments
Labels
area-identity Includes: Identity and providers ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. Status: Resolved

Comments

@glen-84
Copy link
Contributor

glen-84 commented Jan 24, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Setting DefaultScheme in authentication configuration does not appear to work.

Expected Behavior

No response

Steps To Reproduce

Exceptions (if any)

System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).
   at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.<>c__DisplayClass0_0.<<HandleAsync>g__Handle|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

.NET Version

7.0.102

Anything else?

No response

@mkArtakMSFT mkArtakMSFT added the area-identity Includes: Identity and providers label Jan 24, 2023
@captainsafia
Copy link
Member

We removed support for setting the default scheme from configuration in preview7. See #42578. For .NET 7, the default scheme is inferred if there is only one scheme registered on the application. For other scenarios, we wanted to stick with the existing conventions for setting the default scheme.

The fact that it exists in the sample is an artifact of it not being updated in reaction to the change.

@captainsafia captainsafia added ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Jan 24, 2023
@ghost ghost added the Status: Resolved label Jan 24, 2023
@glen-84
Copy link
Contributor Author

glen-84 commented Jan 25, 2023

That's a pity. This one, along with being able to set NameClaimType, would allow me to use:

builder.Services
    .AddAuthentication()
    .AddJwtBearer("FusionAuth")
    .AddJwtBearer("UserJwts");

Instead of:

builder.Services
    .AddAuthentication(builder.Configuration.GetRequiredSection("Authentication")
        .GetValue<string>("DefaultScheme")!)
    .AddJwtBearer(
        "FusionAuth",
        options => options.TokenValidationParameters.NameClaimType = "preferred_username")
    .AddJwtBearer("UserJwts");

@davidfowl
Copy link
Member

var scheme = builder.Configuration["Authentication:DefaultScheme"] ?? throw new InvalidOperationException("Missing default scheme");
builder.Services
    .AddAuthentication(scheme)
    .AddJwtBearer(
        "FusionAuth",
        options => options.TokenValidationParameters.NameClaimType = "preferred_username")
    .AddJwtBearer("UserJwts");

A little refactoring goes a long way in making the code look more readable.

@glen-84
Copy link
Contributor Author

glen-84 commented Jan 25, 2023

Sure. It's still not as clean though. 🙃

@captainsafia
Copy link
Member

captainsafia commented Jan 25, 2023

Another approach is to implement a PostConfigureOptions service that maps the NameClaimType (and any other values you might need) to the options.

@ghost
Copy link

ghost commented Jan 26, 2023

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Jan 26, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Feb 25, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-identity Includes: Identity and providers ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants