2017-08-19 2 views
0

Ich versuche zu verstehen, wie wir den Strong-Typ beim Erstellen der OpenIDConnectOptions verwenden können.Strong-Typ OpenIdConnectOptions für Aspnet-Core 2.0

Ich weiß, wir können starken Typ für Appsettings und andere Elemente implementieren mit POCO-Klasse und IOptions-Implementierung und Zugriff auf diese von Controller-Konstruktor Aber hier ist mein Problem vor dem Controller-Teil. Zur Laufzeit schlägt es fehl.

mit zu beginnen, ich habe startup.configureservice mit:

services.AddAzureADOpenIDAuthentication(Configuration); 

Ich habe Erweiterung Methode für IServiceCollection für AddAzureADOpenIDAuthentication wie:

services.Configure<AzureADOptions>(configuration.GetSection("Authentication:AzureAd")); 

     services.AddSingleton<IOptionsMonitor<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsSetup>(); 

     services.AddAuthentication(auth => 
     { 
      auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; 
      auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
     }) 
     .AddCookie().AddOpenIdConnect(); 

     return services; 

Endlich habe ich AzureADOpenIdConnectOptionsSetup bei der Umsetzung von IOptionsMonitor unten wie :

public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions> 
{ 
    public OpenIdConnectOptions CurrentValue { get; set; } 

    public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureADOptions> azureADOptions) 
    { 
     CurrentValue = new OpenIdConnectOptions(); 

     CurrentValue.ClientId = azureADOptions.CurrentValue.ClientId; 
     CurrentValue.Authority = azureADOptions.CurrentValue.Authority; 
     CurrentValue.CallbackPath = azureADOptions.CurrentValue.CallbackPath; 
    } 

    public OpenIdConnectOptions Get(string name) 
    { 
     return CurrentValue; 
    } 

    public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener) 
    { 
     throw new NotImplementedException(); 
    } 
} 

Wenn ich diesen Code ausführe, trifft er den Konstruktor und OpenIdConnectOptions Get zweimal und durch den Haltepunkt auf Konstruktor-Ebene überprüfe ich, ob die Einstellungen korrekt von azureADOptions in OpenIdConnectOptions CurrentValue übertragen werden. Dennoch bin ich eine Fehlermeldung bekommen (bevor ich Login drücken, bedeutet das Start es selbst)

InvalidOperationException: Geben Sie Autorität, MetadataAddress, Konfiguration oder Konfigurationsmanager zu OpenIdConnectOptions

Ich bin nicht sicher ob ich OpenIdConnectOptions Get (String-Name) korrekt implementiert habe oder nicht. Ein weiterer Zweifel, Wie soll ich OnChange (Aktion Hörer) implementieren, um die Laufzeitänderung appsettings.json

Antwort

1

Für die Wieder OpenIdConnectOptions zu hören, müssen Sie Konfigurationsmanager und einen einfachen Code wie unten initialisieren:

public static class AzureAdAuthenticationBuilderExtensions 
{  
    public static AuthenticationBuilder AddAzureADOpenIDAuthentication(this AuthenticationBuilder builder, IConfiguration configuration) 
    { 
     builder.Services.Configure<AzureAdOptions>(configuration.GetSection("AzureAd")); 

     builder.Services.AddSingleton<IOptionsMonitor<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsSetup>(); 

     builder.Services.AddAuthentication(auth => 
     { 
      auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; 
      auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
     }) 
     .AddOpenIdConnect(); 
     return builder; 
    } 

    public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions> 
    { 
     public OpenIdConnectOptions CurrentValue { get; set; } 
     private IDataProtectionProvider _dataProtectionProvider; 

     public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureAdOptions> azureADOptions,IDataProtectionProvider dataProtectionProvider) 
     { 
      _dataProtectionProvider = dataProtectionProvider; 
      CurrentValue = new OpenIdConnectOptions 
      { 
       ClientId = azureADOptions.CurrentValue.ClientId, 
       Authority = $"{azureADOptions.CurrentValue.Instance}{azureADOptions.CurrentValue.TenantId}", 
       CallbackPath = azureADOptions.CurrentValue.CallbackPath 
      }; 
     } 

     public OpenIdConnectOptions Get(string name) 
     { 
      OpenIdConnectPostConfigureOptions op = new OpenIdConnectPostConfigureOptions(_dataProtectionProvider); 
      op.PostConfigure(name, CurrentValue); 
      return CurrentValue; 
     } 

     public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener) 
     { 
      throw new NotImplementedException(); 
     } 
    }   
} 
Verwandte Themen