It is possible to use multiple JWT Bearer Authentication schemes in C# by registering them as separate authentication handlers with different authentication scheme names in the ConfigureServices method of your Startup class, and then specifying the appropriate authentication scheme in the [Authorize] attribute on your controller or action method.
Here's an example of how to register and use two JWT Bearer Authentication schemes:
public void ConfigureServices(IServiceCollection services) { // Configure the first JWT Bearer Authentication scheme services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = "issuer1", ValidAudience = "audience1", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secret1")) }; }); // Configure the second JWT Bearer Authentication scheme services.AddAuthentication("MyCustomScheme") .AddJwtBearer("MyCustomScheme", options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = "issuer2", ValidAudience = "audience2", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secret2")) }; }); // Add other services... } [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public IActionResult MyControllerAction1() { // This action is protected by the first JWT Bearer Authentication scheme } [Authorize(AuthenticationSchemes = "MyCustomScheme")] public IActionResult MyControllerAction2() { // This action is protected by the second JWT Bearer Authentication scheme } In this example, two different JWT Bearer Authentication schemes are configured using AddAuthentication. The first scheme uses the default authentication scheme name (JwtBearerDefaults.AuthenticationScheme) and is configured to use the parameters for issuer1, audience1, and secret1. The second scheme uses a custom authentication scheme name (MyCustomScheme) and is configured to use the parameters for issuer2, audience2, and secret2.
To use these authentication schemes in your controller or action methods, you can specify the appropriate authentication scheme in the [Authorize] attribute. The first action method (MyControllerAction1) is protected by the first JWT Bearer Authentication scheme, so it uses the default authentication scheme name (JwtBearerDefaults.AuthenticationScheme). The second action method (MyControllerAction2) is protected by the second JWT Bearer Authentication scheme, so it uses the custom authentication scheme name (MyCustomScheme).
"C# use multiple JWT Bearer Authentication in ASP.NET Core"
services.AddAuthentication() .AddJwtBearer("scheme1", options => { // Configure options for the first scheme }) .AddJwtBearer("scheme2", options => { // Configure options for the second scheme }); "ASP.NET Core use multiple JWT Bearer Authentication with policies"
services.AddAuthorization(options => { options.AddPolicy("Policy1", policy => policy.RequireAuthenticatedUser().AddAuthenticationSchemes("scheme1")); options.AddPolicy("Policy2", policy => policy.RequireAuthenticatedUser().AddAuthenticationSchemes("scheme2")); }); "C# use JWT Bearer Authentication for API and user authentication"
services.AddAuthentication() .AddJwtBearer("ApiScheme", options => { // Configure options for API authentication }) .AddJwtBearer("UserScheme", options => { // Configure options for user authentication }); "ASP.NET Core use JWT Bearer Authentication for different audiences"
services.AddAuthentication() .AddJwtBearer("Audience1", options => { // Configure options for the first audience }) .AddJwtBearer("Audience2", options => { // Configure options for the second audience }); "C# use multiple JWT Bearer Authentication with custom claim requirements"
services.AddAuthorization(options => { options.AddPolicy("Policy1", policy => policy.RequireClaim("customClaim1").AddAuthenticationSchemes("scheme1")); options.AddPolicy("Policy2", policy => policy.RequireClaim("customClaim2").AddAuthenticationSchemes("scheme2")); }); "ASP.NET Core use JWT Bearer Authentication for different token issuers"
services.AddAuthentication() .AddJwtBearer("Issuer1", options => { // Configure options for the first token issuer }) .AddJwtBearer("Issuer2", options => { // Configure options for the second token issuer }); "C# use multiple JWT Bearer Authentication with token validation parameters"
services.AddAuthentication() .AddJwtBearer("Scheme1", options => { // Configure options for token validation in the first scheme }) .AddJwtBearer("Scheme2", options => { // Configure options for token validation in the second scheme }); "ASP.NET Core use JWT Bearer Authentication with custom events for each scheme"
services.AddAuthentication() .AddJwtBearer("Scheme1", options => { options.Events = new JwtBearerEvents { // Configure custom events for the first scheme }; }) .AddJwtBearer("Scheme2", options => { options.Events = new JwtBearerEvents { // Configure custom events for the second scheme }; }); "C# use multiple JWT Bearer Authentication with token revocation"
services.AddAuthentication() .AddJwtBearer("Scheme1", options => { // Configure options for token revocation in the first scheme }) .AddJwtBearer("Scheme2", options => { // Configure options for token revocation in the second scheme }); "ASP.NET Core use JWT Bearer Authentication with different signing keys"
services.AddAuthentication() .AddJwtBearer("Key1", options => { // Configure options for the first signing key }) .AddJwtBearer("Key2", options => { // Configure options for the second signing key }); object grid-layout url-rewriting smtp virtual-device-manager jquery-ui dry background-task pausing-execution message-listener