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