英文:
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 => 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, 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 => 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
答案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 => 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"
};
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论