asp.net核心读取appsettings on program.cs combine di
我在我的类中读取程序中的配置。它效果很好。
var builder = WebApplication.CreateBuilder(args);
var authSettingsSection = builder.Configuration.GetSection("Auth");
builder.Services.Configure<AuthConfig>(authSettingsSection);
builder.Services.AddAuthorization();
builder.Services.AddAuthentication();
builder.Services.AddControllers();
<...>
但是,然后我需要将AddAuthentication设置添加到程序中,并且需要配置中的值。如何在计划课中获得?我不想写类似的东西:
var key = builder.Configuration.GetValue<string>("MySection:SecretKey");
也就是说,它将是这样的:
var builder = WebApplication.CreateBuilder(args);
var authSettingsSection = builder.Configuration.GetSection("Auth");
builder.Services.Configure<AuthConfig>(authSettingsSection);
var key = builder.Configuration.GetValue<string>("MySection:SecretKey");
var issuer = builder.Configuration.GetValue<string>("MySection:Issuer");
var audience = builder.Configuration.GetValue<string>("MySection:Audience");
// and more...
builder.Services.AddAuthorization();
builder.Services
.AddAuthentication(opt => {
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey =
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))};
});
builder.Services.AddControllers();
<...>
如何直接读取班级的价值观,而无需冗余?
I read the configuration from appsettings.json in program into my class so that through DI get access to the values in the controller. It works great.
var builder = WebApplication.CreateBuilder(args);
var authSettingsSection = builder.Configuration.GetSection("Auth");
builder.Services.Configure<AuthConfig>(authSettingsSection);
builder.Services.AddAuthorization();
builder.Services.AddAuthentication();
builder.Services.AddControllers();
<...>
But then I needed to add the AddAuthentication settings to the program and I needed the values from the configuration. How can they be obtained in the program class? I don't want to write something like:
var key = builder.Configuration.GetValue<string>("MySection:SecretKey");
That is, it will be something like this:
var builder = WebApplication.CreateBuilder(args);
var authSettingsSection = builder.Configuration.GetSection("Auth");
builder.Services.Configure<AuthConfig>(authSettingsSection);
var key = builder.Configuration.GetValue<string>("MySection:SecretKey");
var issuer = builder.Configuration.GetValue<string>("MySection:Issuer");
var audience = builder.Configuration.GetValue<string>("MySection:Audience");
// and more...
builder.Services.AddAuthorization();
builder.Services
.AddAuthentication(opt => {
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey =
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))};
});
builder.Services.AddControllers();
<...>
How to read values both to the class and directly once without redundancy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论