2017-09-15 3 views
1

私はappsettings.jsonのサービスを設定し、それらをコントローラに注入する方法を理解しています。しかし、Authを設定するときにConfigureServicesの値を使用する必要があります。どうすればいい?私の下のサンプルを見てください。具体的に、この行:startup.csのappsetting.jsonの値にアクセスする

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

答えて

1

あなたが財産

public class AADSettings 
{ 
public string ClientId { get; set; } 
} 

ようとappsettings.jsonファイル内のclientIdを持つPOCOクラスと呼ばれるAADSettingsを持っている必要があります動作するように上記のコードの場合、この

var config = Configuration.GetSection("AADSettings").Get<AADSettings>(); 
option.clientId = config.ClientId; 

のように、このConfigureServicesメソッドにアクセスすることができ、あなたこのようなエントリが必要です

"AADSettings": { 
    "ClientId": "Client1", 
} 
2

は、あなたがこのようなノードの下にそれを持ってあなたのappsettings.jsonにと仮定:

"option": { 
    "clientId": "example client id" 
} 

その後、あなたはそれにアクセスすることができるはずです次のコードを介して

関連する問題