c# - 为什么是 AppSettingsSection .Get一片空白?
问题描述
我正在尝试在 ASP.net 中为我的 web api 实现基于 jwt 的身份验证,我对此还很陌生。
我在获取 Configration.GetSection.Get 时遇到问题(它变为空) 下面的代码显示了我的 Starup.cs 的片段。
请参阅下面的代码以更好地理解。
namespace DBService
{
public class Startup
{
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
private readonly ILogger<Startup> _logger;
public Startup(ILogger<Startup> logger, IConfiguration configuration)
{
_logger = logger;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
//builder.WithOrigins("*");
});
});
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
// configure jwt authentication
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
services.AddScoped<AccountService>();
//other addscoped here
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc().AddControllersAsServices();
var filePath = Path.Combine(System.AppContext.BaseDirectory, "DBService.xml");
//var filePath = "./DBService.xml";
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "C# Backend Microservice", Version = "v1" });
c.IncludeXmlComments(filePath);
c.OperationFilter<FileUploadOperation>();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwagger();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "C# Backend Microservice");
});
app.UseCors(MyAllowSpecificOrigins);
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
/*app.MapWhen(x => (!x.Request.Path.Value.StartsWith("/api") && !x.Request.Path.Value.StartsWith("/swagger")), builder =>
{
app.UseSpa(spa =>
{
spa.Options.SourcePath = "redux-stylemanager";
//spa.UseReactDevelopmentServer(npmScript: "build");
spa.UseReactDevelopmentServer(npmScript: "start");
});
});*/
}
}
}
虽然这是我当前的 appsettings.json
{
"ConnectionStrings": {
"EmailDb": "mongodb://localhost:27017"
},
"AppSettings": {
"Secret": "TEST SECRET"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
最后,我的 AppSettings 类定义如下:
namespace MOHK_DBService.Utilities
{
public class AppSettings
{
public string Secret { get; set; }
}
}
解决方案
推荐阅读
- javascript - Firebase 管理员:代表第三方管理 firebase 数据库
- actions-on-google - 发送 NEW_SURFACE 意图的通知时,Google 监听模式处于开启状态
- pine-script - 如何在 pine 脚本中获取特殊栏的编号
- c# - 如何在简单的类库中使用实体框架工具进行迁移
- c++ - 如果您知道我的意思,有没有办法在背景中为 C++(控制台)中的某些区域着色?
- javascript - React 中的状态不随 setState 方法改变
- r - 如何创建一个“不等于”的函数?
- python-3.x - 如何更新 Flask-SQLAlchemy-Marshmallow 中的嵌套对象
- sql - SSDT 数据库项目中对系统数据库的引用变得重复
- php - PHP | 获取数组中的值 | WordPress