Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOptionsMonitor<MyOptions> contains empty options when HandleAuthenticateAsync() is called

I've created my own authentication handler in ASP.NET Core MVC v2.0.

It is invoked OK, but the options object is empty when the HandleAuthenticateAsync method is invoked.

Startup config

authenticationBuilder.AddSingleSignOn("SingleSignOn", options =>
{
    options.OpenGlobalDb = () => OpenGlobalConnection(configuration);
    options.OpenTenantDb = orgId => OpenTenantDb(configuration, orgId);
    options.AuthenticationScheme = "Cookies";
});

(I've had a breakpoint in the code and it's invoked)

The authentication handler

public class SingleSignOnAuthenticationHandler : AuthenticationHandler<SingleSignOnAuthenticationOptions>,
    IAuthenticationRequestHandler
{

    private IOptionsMonitor<SingleSignOnAuthenticationOptions> _options;

    public SingleSignOnAuthenticationHandler(IOptionsMonitor<SingleSignOnAuthenticationOptions> options,
        ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
    {
        _options = options;
    }

    protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        // _options.CurrentValue.OpenGlobalDb is null here.
    }

    //[.. all other methods ..]
}

Auth builder extensions

public static class AuthenticationBuilderExtensions
{
    public static AuthenticationBuilder AddSingleSignOn(this AuthenticationBuilder builder, string authenticationScheme, Action<SingleSignOnAuthenticationOptions> configureOptions)
    {
        return builder.AddScheme<SingleSignOnAuthenticationOptions, SingleSignOnAuthenticationHandler>(authenticationScheme, "SingleSignOn authentication", configureOptions);
    }
}
like image 971
jgauffin Avatar asked Nov 25 '25 12:11

jgauffin


1 Answers

Do not save an own version of the options object in the constructor. Use the Options property in the HandleAuthenticateAsync method instead.

like image 173
jgauffin Avatar answered Nov 28 '25 03:11

jgauffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!