访问 startup.cs 中的 appsetting.json 个值
accessing appsetting.json values in startup.cs
我了解如何为 appsettings.json 配置服务并将它们注入控制器。但是,我需要在配置 Auth 时使用 ConfigureServices 中的值。我该怎么做?请参阅下面的示例。特别是这一行:
option.clientId = /*Need client Id from appsettings.json*/
代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<AADSettings>(Configuration.GetSection("AADSettings"));
services.Configure<APISettings>(Configuration.GetSection("APISettings"));
// Add Authentication services.
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
// Configure the OWIN pipeline to use cookie auth.
.AddCookie()
// Configure the OWIN pipeline to use OpenID Connect auth.
.AddOpenIdConnect(option =>
{
option.clientId = /*Need client Id from appsettings.json*/
option.Events = new OpenIdConnectEvents
{
OnRemoteFailure = OnAuthenticationFailed,
};
});
}
假设在您的 appsettings.json 中,它位于这样的节点下:
"option": {
"clientId": "example client id"
}
那么您应该可以通过以下代码访问它
option.clientId = Configuration["option:clientId"]
您可以像这样访问此 ConfigureServices 方法
var config = Configuration.GetSection("AADSettings").Get<AADSettings>();
option.clientId = config.ClientId;
要使上述代码正常工作,您需要让 POCO class 调用 AADSettings 并将 ClientId 作为 属性
public class AADSettings
{
public string ClientId { get; set; }
}
并且在 appsettings.json 文件中,您需要有一个这样的条目
"AADSettings": {
"ClientId": "Client1",
}
Startup.cs :
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment)
{
User = Configuration.GetSection("HangfireSettings:UserName").Value,
Pass = Configuration.GetSection("HangfireSettings:Password").Value
}
}
appsettings.json:
"HangfireSettings": {
"UserName": "admin",
"Password": "admin"
},
我了解如何为 appsettings.json 配置服务并将它们注入控制器。但是,我需要在配置 Auth 时使用 ConfigureServices 中的值。我该怎么做?请参阅下面的示例。特别是这一行:
option.clientId = /*Need client Id from appsettings.json*/
代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<AADSettings>(Configuration.GetSection("AADSettings"));
services.Configure<APISettings>(Configuration.GetSection("APISettings"));
// Add Authentication services.
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
// Configure the OWIN pipeline to use cookie auth.
.AddCookie()
// Configure the OWIN pipeline to use OpenID Connect auth.
.AddOpenIdConnect(option =>
{
option.clientId = /*Need client Id from appsettings.json*/
option.Events = new OpenIdConnectEvents
{
OnRemoteFailure = OnAuthenticationFailed,
};
});
}
假设在您的 appsettings.json 中,它位于这样的节点下:
"option": {
"clientId": "example client id"
}
那么您应该可以通过以下代码访问它
option.clientId = Configuration["option:clientId"]
您可以像这样访问此 ConfigureServices 方法
var config = Configuration.GetSection("AADSettings").Get<AADSettings>();
option.clientId = config.ClientId;
要使上述代码正常工作,您需要让 POCO class 调用 AADSettings 并将 ClientId 作为 属性
public class AADSettings
{
public string ClientId { get; set; }
}
并且在 appsettings.json 文件中,您需要有一个这样的条目
"AADSettings": {
"ClientId": "Client1",
}
Startup.cs :
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment)
{
User = Configuration.GetSection("HangfireSettings:UserName").Value,
Pass = Configuration.GetSection("HangfireSettings:Password").Value
}
}
appsettings.json:
"HangfireSettings": {
"UserName": "admin",
"Password": "admin"
},