title: How do I setup multiple auth schemes in ASP.NET Core 2.0?
body: <p>I'm trying to migrate my auth stuff to Core 2.0 and having an issue using my own authentication scheme. My service setup in startup looks like this:</p>
  
  <pre><code>var authenticationBuilder = services.AddAuthentication(options =&gt;
  {
      options.AddScheme("myauth", builder =&gt;
      {
          builder.HandlerType = typeof(CookieAuthenticationHandler);
      });
  })
      .AddCookie();
  </code></pre>
  
  <p>My login code in the controller looks like this:</p>
  
  <pre><code>var claims = new List&lt;Claim&gt;
  {
      new Claim(ClaimTypes.Name, user.Name)
  };
  
  var props = new AuthenticationProperties
  {
      IsPersistent = persistCookie,
      ExpiresUtc = DateTime.UtcNow.AddYears(1)
  };
  
  var id = new ClaimsIdentity(claims);
  await HttpContext.SignInAsync("myauth", new ClaimsPrincipal(id), props);
  </code></pre>
  
  <p>But when I'm in a controller or action filter, I only have one identity, and it's not an authenticated one:</p>
  
  <pre><code>var identity = context.HttpContext.User.Identities.SingleOrDefault(x =&gt; x.AuthenticationType == "myauth");
  </code></pre>
  
  <p>Navigating these changes has been difficult, but I'm guessing that I'm doing .AddScheme wrong. Any suggestions?</p>
  
  <p>EDIT: Here's (essentially) a clean app that results not in two sets of Identities on User.Identies:</p>
  
  <pre><code>namespace WebApplication1.Controllers
  {
      public class Testy : Controller
      {
          public IActionResult Index()
          {
              var i = HttpContext.User.Identities;
              return Content("index");
          }
  
          public async Task&lt;IActionResult&gt; In1()
          {
              var claims = new List&lt;Claim&gt; { new Claim(ClaimTypes.Name, "In1 name") };
              var props = new AuthenticationProperties  { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddYears(1) };
              var id = new ClaimsIdentity(claims);
              await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id), props);
              return Content("In1");
          }
  
          public async Task&lt;IActionResult&gt; In2()
          {
              var claims = new List&lt;Claim&gt; { new Claim(ClaimTypes.Name, "a2 name") };
              var props = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddYears(1) };
              var id = new ClaimsIdentity(claims);
              await HttpContext.SignInAsync("a2", new ClaimsPrincipal(id), props);
              return Content("In2");
          }
  
          public async Task&lt;IActionResult&gt; Out1()
          {
              await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
              return Content("Out1");
          }
  
          public async Task&lt;IActionResult&gt; Out2()
          {
              await HttpContext.SignOutAsync("a2");
              return Content("Out2");
          }
      }
  }
  </code></pre>
  
  <p>And Startup:</p>
  
  <pre><code>namespace WebApplication1
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddAuthentication(options =&gt;
              {
                  options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                  })
                  .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
                  .AddCookie("a2");
  
              services.AddMvc();
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              app.UseAuthentication();
  
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
              });
          }
      }
  }
  </code></pre>