2017-10-03 6 views
2

すべてIdentityServer4の例は、設定時にAuthorityプロパティをハードコード:IdentityServer4:環境に基づいて権限を設定する方法は?

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

どのように私は、環境(すなわち、ステージングとプロダクション)に基づいて権限をロードしますか?

+0

この場合、Env.IsStaging()とEnv.IsProduction()は十分ではありませんか? –

+0

@DanNguyen私はブールインラインに基づいてURLをハードコードしたくありません。私は彼らがいくつかの設定ファイルや環境変数を介して読み込まれるべきだと思います。 – Bob

答えて

1

これは、我々が何をすべきかです:

は、私たちは、それぞれの環境ごとに異なるappSettings.jsonファイルを持っています。

enter image description here

すべてのファイルがIdentityServer用に別の値が含まれています。例えば

{ 
    "IdentityServerSettings": { 
    "Authority": "http://localhost:5000", 
    "ApiName": "tb5api" 
    } 
} 

その後Startup.csクラスで、我々は現在の環境に基づいて設定JSONファイルをロードします。そして、あなたが必要な場所IdentityServerSettingsあなたが設定方法コントローラにORでそれらを注入することができ

/// <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; } 
    } 

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")); 
...... 

はその後、我々はに私達の設定をロードするクラスを持っている

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", 
      }); 

....... 
+0

appsettings.jsonからホストをロードすると、Webホストを設定するときにどのようにprogram.csに設定されますか?あなたのコードから、あなたはappsettings.jsonから権限を手動で設定しているように見えますが、Webホストを設定するときにはデフォルトのホストを使用しているだけです。 – Bob

+0

アイデンティティサーバーまたはクライアントWebサイトの@Bobウェブホスト? –

+0

@Bobこれは、プログラム起動時に設定するために探しているものだと思います。https://stackoverflow.com/a/43265773/1910735 –

関連する問題