Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Strong-type OpenIdConnectOptions for aspnet core 2.0

$
0
0

I am trying to understand How can we utilize the strong-type when constructing the OpenIDConnectOptions.

I know we can do the strong type casting for appsettings and other settings and can utilize that in our constructor through IOptions but this is before the controller method.

To Start with, I have startup.configureservice with:

services.AddAzureADOpenIDAuthentication(Configuration);

I have Extension method for IServiceCollection for AddAzureADOpenIDAuthentication like:

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;

Finally I have AzureADOpenIdConnectOptionsSetup with the implementation of IOptionsMonitor like below:

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

When I run this code, It hit the Constructor and OpenIdConnectOptions Get twice and through the breakpoint at constructor level, I check the settings are correctly transferred from azureADOptions to OpenIdConnectOptions CurrentValue. Still, I am getting an error message (before I press login, it means startup it self)

InvalidOperationException: Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions

I am not sure, whether I have correctly implemented OpenIdConnectOptions Get(string name) or not. One more doubt is, How should I implement OnChange(Action listener) to listen the run-time change of appsettings.json


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>