using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; namespace zero.Identity; public class ZeroAuthenticationSchemeProvider : AuthenticationSchemeProvider { readonly IHttpContextAccessor httpContextAccessor; public ZeroAuthenticationSchemeProvider(IHttpContextAccessor httpContextAccessor, IOptions options) : base(options) { this.httpContextAccessor = httpContextAccessor; } private async Task GetRequestSchemeAsync() { var request = httpContextAccessor.HttpContext?.Request; if (request == null) { throw new ArgumentNullException("The HTTP request cannot be retrieved."); } // For API requests, use authentication tokens. if (request.Path.StartsWithSegments("/zero")) // TODO from options. but this one needs to be a singleton { return await GetSchemeAsync(Constants.Auth.BackofficeScheme); } // For the other requests, return null to let the base methods // decide what's the best scheme based on the default schemes // configured in the global authentication options. return null; } public override async Task GetDefaultAuthenticateSchemeAsync() => await GetRequestSchemeAsync() ?? await base.GetDefaultAuthenticateSchemeAsync(); public override async Task GetDefaultChallengeSchemeAsync() => await GetRequestSchemeAsync() ?? await base.GetDefaultChallengeSchemeAsync(); public override async Task GetDefaultForbidSchemeAsync() => await GetRequestSchemeAsync() ?? await base.GetDefaultForbidSchemeAsync(); public override async Task GetDefaultSignInSchemeAsync() => await GetRequestSchemeAsync() ?? await base.GetDefaultSignInSchemeAsync(); public override async Task GetDefaultSignOutSchemeAsync() => await GetRequestSchemeAsync() ?? await base.GetDefaultSignOutSchemeAsync(); }