Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created October 20, 2022 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamianEdwards/577b773778bca7aff61890679d69c9f0 to your computer and use it in GitHub Desktop.
Save DamianEdwards/577b773778bca7aff61890679d69c9f0 to your computer and use it in GitHub Desktop.
Disable the `AuthenticationHandler`'s new behavior of setting the default authentication scheme to the single scheme registered when there is only one
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthentication().AddJwtBearer();
if (builder.Services.FirstOrDefault(r => r.ServiceType == typeof(IAuthenticationSchemeProvider)
&& r.ImplementationType == typeof(AuthenticationSchemeProvider)) is not null)
{
// Default AuthenticationSchemeProvider is registered, override that with a wrapper that disables its behavior of setting the
// default authentication scheme when only one scheme is registered.
builder.Services.AddSingleton<IAuthenticationSchemeProvider, DisableAutoDefaultSchemeAuthenticationSchemeWrapper>();
}
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/", async (IAuthenticationSchemeProvider authenticationSchemeProvider) =>
(await authenticationSchemeProvider.GetDefaultAuthenticateSchemeAsync())?.Name ?? "[null]");
app.Run();
internal class DisableAutoDefaultSchemeAuthenticationSchemeWrapper : IAuthenticationSchemeProvider
{
private readonly AuthenticationSchemeProvider _authenticationSchemeProvider;
private readonly AuthenticationOptions _options;
public DisableAutoDefaultSchemeAuthenticationSchemeWrapper(IOptions<AuthenticationOptions> authnOptions)
{
_authenticationSchemeProvider = new(authnOptions);
_options = authnOptions.Value;
}
public void AddScheme(AuthenticationScheme scheme) => _authenticationSchemeProvider.AddScheme(scheme);
public Task<IEnumerable<AuthenticationScheme>> GetAllSchemesAsync() => _authenticationSchemeProvider.GetAllSchemesAsync();
public Task<AuthenticationScheme?> GetDefaultAuthenticateSchemeAsync() =>
_options.DefaultAuthenticateScheme is not null
? GetSchemeAsync(_options.DefaultAuthenticateScheme)
: GetDefaultSchemeAsync();
public Task<AuthenticationScheme?> GetDefaultChallengeSchemeAsync() =>
_options.DefaultAuthenticateScheme is not null
? GetSchemeAsync(_options.DefaultAuthenticateScheme)
: GetDefaultSchemeAsync();
public Task<AuthenticationScheme?> GetDefaultForbidSchemeAsync() =>
_options.DefaultForbidScheme is not null
? GetSchemeAsync(_options.DefaultForbidScheme)
: GetDefaultSchemeAsync();
public Task<AuthenticationScheme?> GetDefaultSignInSchemeAsync() =>
_options.DefaultSignInScheme is not null
? GetSchemeAsync(_options.DefaultSignInScheme)
: GetDefaultSchemeAsync();
public Task<AuthenticationScheme?> GetDefaultSignOutSchemeAsync() =>
_options.DefaultSignOutScheme is not null
? GetSchemeAsync(_options.DefaultSignOutScheme)
: GetDefaultSchemeAsync();
public Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync() =>
_authenticationSchemeProvider.GetRequestHandlerSchemesAsync();
public Task<AuthenticationScheme?> GetSchemeAsync(string name) =>
_authenticationSchemeProvider.GetSchemeAsync(name);
public void RemoveScheme(string name) => _authenticationSchemeProvider.RemoveScheme(name);
private Task<AuthenticationScheme?> GetDefaultSchemeAsync()
=> _options.DefaultScheme != null
? GetSchemeAsync(_options.DefaultScheme)
: Task.FromResult<AuthenticationScheme?>(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment