Auth0 Home Blog Docs

Token authentication for multiple Auth0 domain

api
webapi-authorization
#1

I have 2 Auth0 accounts which generating the jwt token when client login from either of the account. I already know how to authenticate the token in my Web API for 1 domain and it is working fine. I want to know how to authenticate for 2 Auth0 domains. Right now it is authenticating all the users on tldev111.auth0.com domain and i want to authenticate users on tldev123.auth0.com domain too

Startup.cs–

public void Configuration(IAppBuilder app)
    {
        var domain = $"https://{ConfigurationManager.AppSettings["Auth0Domain"]}/";
        var apiIdentifier = ConfigurationManager.AppSettings["Auth0ApiIdentifier"];

        var keyResolver = new OpenIdConnectSigningKeyResolver(domain);
        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidAudience = apiIdentifier,
                    ValidIssuer = domain,
                    IssuerSigningKeyResolver = (token, securityToken, kid, parameters) => keyResolver.GetSigningKey(kid)
                }
            });

        // Configure Web API
        WebApiConfig.Configure(app);
    }

WebApiConfig -

public static void Configure(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });

        app.UseWebApi(config);
    }

web.config app settings–

<appSettings>
<add key="Auth0Domain" value="tldev111.auth0.com" />
<add key="Auth0ApiIdentifier" value="https://aaa/SRTAPI/api" />
#3

Take a look at https://github.com/auth0-samples/auth0-aspnetcore-webapi-samples/tree/master/Samples/multiple-issuer for a sample with a custom MultipleIssuerSigningKeyResolver.

The readme.md file shows how to initialize the JWT middleware with more than one issuer (Auth0 domain).

2 Likes
#4

Thank you. That helped.

2 Likes
closed #5

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.