title: Cookie authentication issue ASP.NET CORE 2.1. AWS load balancing
body: <p><strong>UPDATE:</strong> It looks like following code is failing:</p>
  
  <pre><code>services.AddDataProtection()
              .SetApplicationName(appname)
              .PersistKeysToRedis(redis, "DataProtectionKeys")
              .ProtectKeysWithCertificate(LoadCert(Configuration));
  </code></pre>
  
  <p>It's unable to read certificate from the pfx file. </p>
  
  <p><strong>UPDATE2:</strong>
   Oh my! Certificate file has been excluded by .gitignore!:))  Live&amp;Learn. At least we lived, right!?;))</p>
  
  <hr>
  
  <p><strong>Initial question:</strong>
  I have ASP.NET Core 2.1 app deployed behind AWS load balancer in Docker container.
  When I'm trying to log in into app from the login page. I am getting InvalidOperationException with this justification: </p>
  
  <blockquote>
    <p>No authenticationScheme was specified, and there was no
    DefaultChallengeScheme found.</p>
  </blockquote>
  
  <p>But when I hit same URL again, it actually moving to the proper page and works for a while, then again throws same exception with HTTP status 500
  and after 2nd attempt to open same page it succeeds. Interestingly enough Chrome is not as robust as IE: if IE unable to recover after exception Chrome always return 404 on subsequent submission of the page, which produced aforementioned exception.</p>
  
  <blockquote>
    <p>So I would appreciate, if somebody would be able to provide me with
    ideas how to remedy the situation Obviously issue is related to the
    authentication, but I could not figure out exactly what should be done.</p>
  </blockquote>
  
  <p>Here is relevant exert from ConfigureServices() in Startup.cs:</p>
  
  <pre><code>        string appname = "MyApp";
          var redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("RedisConnection"));
          services.AddDataProtection()
              .SetApplicationName(appname)
              .PersistKeysToRedis(redis, "DataProtectionKeys")
              .ProtectKeysWithCertificate(LoadCert(Configuration));
  
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
          services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme)
                  .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
                      options =&gt;
                      { 
                          options.LoginPath = new PathString("/Area/Ctrl/Login");
                          options.LogoutPath = new PathString("/Area/Ctrl/Logout");
                          options.Cookie.IsEssential = true;
                      });
  
          services.AddDistributedRedisCache(o =&gt;
          {
              o.Configuration = Configuration.GetConnectionString("RedisConnection");
          });
          services.AddSession(options =&gt;
          {
              options.Cookie.Name = appname;
              options.IdleTimeout = TimeSpan.FromSeconds(600);
          });
  </code></pre>
  
  <p>Here is relevant code from Configure() in Startup.cs:</p>
  
  <pre><code>        app.UseForwardedHeaders();
          app.UseHttpsRedirection();
          app.UseStaticFiles();
          app.UseAuthentication();
          app.UseSession();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
               name: "areas",
               template: "{area:exists}/{controller=Ctrl}/{action=Login}/{id?}"
             );
          }); 
  </code></pre>
  
  <p>Here is how I am setting principal in controller, which handles login:</p>
  
  <pre><code>            ClaimsIdentity identity = new ClaimsIdentity(GetUserRoleClaims(dbUserData), CookieAuthenticationDefaults.AuthenticationScheme);
              ClaimsPrincipal principal = new ClaimsPrincipal(identity);
              if (principal == null)
                  throw new ApplicationException($"Could not create principal for {user?.UserName} user.");
  
              await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
              if (httpContext.User == null)
              {
                  httpContext.User = principal;
              }
  </code></pre>