title: ASP.NET Core using custom authenticationhandler with cookieauthentication
body: <p>I'm trying to create my own AuthenticationHandler and use with cookie authentication:</p>
  
  <pre><code>services.AddAuthentication(options =&gt;
    {
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = MyAuth.Scheme;
    })
    .AddCookie()
    .AddScheme&lt;MyAuthenticationOptions, MyAuthenticationHandler&gt;(MyAuth.Scheme, "My auth scheme", options =&gt; { });
  </code></pre>
  
  <p>. </p>
  
  <pre><code>public MyAuthenticationHandler(...) : base(...) {}
  
      protected override Task&lt;AuthenticateResult&gt; HandleAuthenticateAsync()
      {
          throw new NotImplementedException();  
      }    
  
      protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
      {                     
          var myUser = await DoAuth();
  
          if (!myUser.IsAuthenticated)
          {
              if (Context.Request.Query.ContainsKey("isRedirectedFromSSO"))
              {
                  Context.Response.Redirect("/unauthorized");
                  return;
              }
              else
              {
                  Context.Response.Redirect("url to sso");
                  return;
              }              
          }    
  
          var claims = new List&lt;Claim&gt;
          {
              new Claim(ClaimTypes.NameIdentifier, user.Username),            
          };
  
          var identity = new ClaimsIdentity(claims, MyAuth.Scheme);
          var claimsPrincipal = new ClaimsPrincipal(identity);
  
          var authProperties = new AuthenticationProperties {};
  
          await Context.SignInAsync(
              CookieAuthenticationDefaults.AuthenticationScheme,
              claimsPrincipal,
              authProperties);
  
          Context.Response.Redirect(Request.GetEncodedUrl());
      }
  }
  </code></pre>
  
  <ol>
  <li>If there is a valid auth cookie, auth using that </li>
  <li>If there is not a valid auth cookie, challenge using my auth and create an auth cookie if successful</li>
  </ol>
  
  <p>This works in practice, but I find it a little weird I'm doing the actual authentication in <code>HandleChallenge</code> and redirecting if it fails.
  It also seems odd to me to be calling one AuthenticationHandler (cookie) from another (MyAuthenticationHandler).</p>
  
  <p><strong>How can I set this up correctly so that I'm doing the implementation in <code>HandleAuthenticate</code> instead?</strong>
  With my current implementation that method is never actually called.</p>
  
  <p>Also, <strong>is it ok to call one authentication handler from another?</strong></p>
  
  <p>P.S. I have looked at several other posts and articles (including <a href="https://stackoverflow.com/questions/46938511/asp-net-core-2-0-custom-middleware-using-cookie-authentication">this</a>, <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-2.0&amp;tabs=aspnetcore2x" rel="noreferrer">this</a> and <a href="https://joonasw.net/view/creating-auth-scheme-in-aspnet-core-2" rel="noreferrer">this</a>), but I have not been able to find the answers to my questions from looking at them. Any help would be appreciated.</p>