【问题标题】:IdentityServer4 : how to set Authority based on environment?IdentityServer4:如何根据环境设置权限?
【发布时间】:2017-10-03 13:13:43
【问题描述】:

所有 IdentityServer4 示例在配置期间硬编码 Authority 属性:

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:5000";
                options.ApiName = "api";
                options.RequireHttpsMetadata = Env.IsStaging() || Env.IsProduction();
            });

我将如何根据环境(即登台和生产)加载授权?

【问题讨论】:

  • 在这种情况下 Env.IsStaging() 和 Env.IsProduction() 还不够吗?
  • @DanNguyen 我不想基于内联布尔值硬编码网址。我认为它们应该通过一些配置文件或环境变量来加载。

标签: c# asp.net identityserver4


【解决方案1】:

这就是我们所做的:

每个环境都有不同的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",
            });

.......

【讨论】:

  • 如果从 appsettings.json 加载主机,在配置 webhost 时如何在 program.cs 中设置它们?从您的代码来看,您似乎是从 appsettings.json 手动设置权限,但在配置 webhost 时您只是使用默认主机。
  • @Bob 身份服务器或客户端网站的虚拟主机?
  • @Bob 我认为这是您在程序启动中设置设置所要查找的内容:stackoverflow.com/a/43265773/1910735
  • 正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 2018-02-17
相关资源
最近更新 更多