asp.net核心读取appsettings on program.cs combine di

发布于 2025-01-31 01:33:05 字数 1778 浏览 2 评论 0原文

我在我的类中读取程序中的配置。它效果很好。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文