这就是我们所做的:
每个环境都有不同的appSettings.json 文件。
所有文件都包含不同的 IdentityServer 值。例如
{
"IdentityServerSettings": {
"Authority": "http://localhost:5000",
"ApiName": "tb5api"
}
}
然后在 Startup.cs 类中我们根据当前环境加载设置 json 文件。
private readonly IHostingEnvironment _env;
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
_env = env;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<IdentityServerSettings>(Configuration.GetSection("IdentityServerSettings"));
......
然后我们有一个类来加载我们的设置:
/// <summary>
/// This class is a representation of the configuration of the API for Identity Server
/// </summary>
public class IdentityServerSettings
{
// Authority is the Identity Server URL
public string Authority { get; set; }
// Current API/Resource Name
public string ApiName { get; set; }
}
然后,无论您需要IdentityServerSettings,您都可以将它们注入控制器或配置方法中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
#region Identity Server Config
var identityServerOptions = app.ApplicationServices.GetService<IOptions<IdentityServerSettings>>().Value;
// Setup Identity Server Options for this API -
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = identityServerOptions.Authority,
RequireHttpsMetadata = false,
ApiName = identityServerOptions.ApiName,
NameClaimType = "username",
});
.......