0

I'm trying to make a .net core AuthenticationHandler with some custom logic. Whenever I make a request to the page everything in the auth handler runs fine but it returns a 200 without actually executing the code for my end controller. I've distilled it down to this simplified version.

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication("Dummy")
        .AddScheme<AuthenticationSchemeOptions, DummyAuthHandler>("Dummy", null);
    ...

My handler:

public class DummyAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public DummyAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, 
        ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : 
            base(options, logger, encoder, clock)
    {
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
        => Task.FromResult(AuthenticateResult.Success(
               new AuthenticationTicket(new ClaimsPrincipal(), "Dummy")));

    protected override Task HandleChallengeAsync(AuthenticationProperties properties)  
        => Task.CompletedTask;
}

I'm thinking I'm missing one of the methods needed to tell the framework to continue processing the request, and not to just think my authentication handler wants the page redirected. Maybe I even need to add a call to next() somewhere?

user433342
  • 628
  • 5
  • 19

1 Answers1

0

Cracked it thanks to this and this... the ClaimsIdentity was returning IsAuthenticated = False, which is why I thought I needed the HandleChallengeAsync in the first place. Here's what the fixed handler looks like:

public class DummyAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public DummyAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, 
        ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : 
            base(options, logger, encoder, clock)
    {
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
        => Task.FromResult(AuthenticateResult.Success(
               new AuthenticationTicket(new ClaimsPrincipal(
                   new ClaimsIdentity("abc")), "Dummy")));
}
user433342
  • 628
  • 5
  • 19