Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

399
Visualizações
Using multiple authentication providers in C# .net core

We had .net core API already authenticating with AzureAd and then a new requirement came to authenticate the same API using Auth0 as well while keeping existing users access with AzureAd. without any selection of the authentication schema API should be able to authentication using Authorize token in HTTP header token can be either Auth0 or AzureAD.

I found several helpful articles managed to build my solution using them, I thought it better to write the final solution in one place so it might help others who come across the same requirement.

The Following applies in Startup.cs (in ConfigureServices method)

Adding multiple authentication schemes

We need to have one JWT bearer authentication registered as the default authentication scheme. Second one registered with a unique name.

{
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.Audience = "dgdf7g6967-97fdgd";
            options.Authority = "https://login.microsoftonline.com/myazure.abc.com";
        })
        .AddJwtBearer("Auth0", options =>
        {
            options.Audience = "myweb.abc.com";
            options.Authority = "https://my.auth0.com";
        });
}

Then we need to update the default authorization policy to accept both authentication schemes.

services.AddAuthorization(options =>
            {
                var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(
                    JwtBearerDefaults.AuthenticationScheme, "Auth0");
                defaultAuthorizationPolicyBuilder =
                defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();
                options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build();
            });

Once that's done you need to AddIdentities for both schemas. I've created separate middleware to handle this

MultipleSchemaAuthenticationMiddleware.cs

public async Task InvokeAsync(HttpContext context)
        {
            var principal = new ClaimsPrincipal();

            var resultAzureAD = await context.AuthenticateAsync();
            if (resultAzureAD?.Principal != null)
            {
                principal.AddIdentities(resultAzureAD.Principal.Identities);
            }
           
            var resultAuth0 = await context.AuthenticateAsync(AppHelper.Settings.Auth0.SchemaName);
            if (resultAuth0?.Principal != null)
            {
                principal.AddIdentities(resultAuth0.Principal.Identities);
            }

            context.User = principal;

            await _next(context);
        }

and register the middleware in Startup.cs (in Configure method)

app.UseMiddleware<MultipleSchemaAuthenticationMiddleware>();

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda