How to set JwtBearerOptions after calling function AddJwtBearer(jwtBearerScheme, o => { }); with empty opts in asp.net core?

huangapple go评论77阅读模式
英文:

How to set JwtBearerOptions after calling function AddJwtBearer(jwtBearerScheme, o => { }); with empty opts in asp.net core?

问题

I'm using Asp.net Core 3.1. While creating a new web project using Visual Studio we select API template and Change Authentication to Individual User Accounts and configure Azure ADB2C options and finally create the project.

In our generated Startup.cs file, we have following function:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
        .AddAzureADB2CBearer(options => Configuration.Bind("AzureAdB2C", options));
    services.AddControllers();
}

The implementation of AddAzureADB2CBearer() can be found [here][1]. This implementation has a line of code is as follows:

builder.Services.Configure(scheme, configureOptions);
    builder.AddJwtBearer(jwtBearerScheme, o => { });

In the above line, it's adding JwtBearer with empty JwtBearerOptions. And Authentication is working perfect. But I want to set some JwtBearerOptions after the following line of code:

services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
   .AddAzureADB2CBearer(options => Configuration.Bind("AzureAdB2C", options));

Is there any way to configure those options after executing the above two lines? I tried the following lines but nothing worked:

services.Configure<JwtBearerOptions>(options =>
{
     options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
     options.Events = new JwtBearerEvents
     {
         OnAuthenticationFailed = AuthenticationFailed
     };

     options.TokenValidationParameters = new TokenValidationParameters()
     {
          ValidateLifetime = true,
          ValidateAudience = true,
          ValidAudience = "myAudience"
      };
});

Please note that I can use AddJwtBearer function and pass options after setting them. But I want to use Microsoft default implementation. And just need to update my JwtBearerOptions that was passed to AddJwtBearer as an argument.
[1]: https://github.com/aspnet/AADIntegration/blob/master/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2CAuthenticationBuilderExtensions.cs

英文:

I'm using Asp.net Core 3.1. While creating a new web project using Visual Studio we select API template and Change Authentication to Individual User Accounts and configure Azure ADB2C options and finally create the project.

In our generated Startup.cs file, we have following function:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
        .AddAzureADB2CBearer(options =&gt; Configuration.Bind(&quot;AzureAdB2C&quot;, options));
    services.AddControllers();
}

The implementation of AddAzureADB2CBearer() can be found [here][1]. This implementation has a line of code is as follows:

builder.Services.Configure(scheme, configureOptions);
    builder.AddJwtBearer(jwtBearerScheme, o =&gt; { });

In the above line, its adding JwtBearer with empty JwtBearerOptions. And Authentication is working perfect. But I want to set some JwtBearerOptions after the following line of code:

services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
   .AddAzureADB2CBearer(options =&gt; Configuration.Bind(&quot;AzureAdB2C&quot;, options));

Is there any way to configure those options after executing the above two lines? I tried the following lines but nothing worked.

services.Configure&lt;JwtBearerOptions&gt;(options =&gt;
{
     options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
     options.Events = new JwtBearerEvents
     {
         OnAuthenticationFailed = AuthenticationFailed
     };

     options.TokenValidationParameters = new TokenValidationParameters()
     {
          ValidateLifetime = true,
          ValidateAudience = true,
          ValidAudience = &quot;myAudience&quot;
      };
});

Please note that I can use AddJwtBearer function and pass options after setting them. But I want to use Microsoft default implementation. And just need to update my JwtBearerOptions that was passed to AddJwtBearer as an argument.
[1]: https://github.com/aspnet/AADIntegration/blob/master/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2CAuthenticationBuilderExtensions.cs

答案1

得分: 2

AddAzureADB2CBearer 后,您可以尝试覆盖特定的模式:

services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
    .AddAzureADB2CBearer(options => Configuration.Bind("AzureAdB2C", options));

services.Configure<JwtBearerOptions>(AzureADB2CDefaults.JwtBearerAuthenticationScheme, options =>
{
    options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
    options.Events = new JwtBearerEvents
    {
        OnAuthenticationFailed = AuthenticationFailed
    };

    options.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidateLifetime = true,
        ValidateAudience = true,
        ValidAudience = "myAudience"
    };
});
英文:

You can try to override the specific schema after AddAzureADB2CBearer:

services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
    .AddAzureADB2CBearer(options =&gt; Configuration.Bind(&quot;AzureAdB2C&quot;, options));

services.Configure&lt;JwtBearerOptions&gt;(AzureADB2CDefaults.JwtBearerAuthenticationScheme, options =&gt;
{
    options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
    options.Events = new JwtBearerEvents
    {
        OnAuthenticationFailed = AuthenticationFailed
    };

    options.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidateLifetime = true,
        ValidateAudience = true,
        ValidAudience = &quot;myAudience&quot;
    };
});

huangapple
  • 本文由 发表于 2020年1月6日 15:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定