title: ASP.NET Core Razor Pages Cookie Authentication redirects to the login page after successful login
body: <p>I followed the article <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-2.1" rel="nofollow noreferrer">Use cookie authentication without ASP.NET Core Identity</a> and downloaded the sample from <a href="https://github.com/aspnet/Docs/tree/master/aspnetcore/security/authentication/cookie/samples" rel="nofollow noreferrer">2.x/Cookies</a>.</p>
  
  <p>Ran the sample in VS 2017. Opened the "contact" page as directed in the documentation and from code (that it is protected), signed in using the credentials authenticated in the code using simple string comparison, it signs in if debugged, which means it adds user principal with its claims in but redirects back to log-in page instead of the contact page.</p>
  
  <p><strong>ConfigureServices:</strong></p>
  
  <pre><code>        services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
          {
              options.CheckConsentNeeded = context =&gt; true;
              options.MinimumSameSitePolicy = SameSiteMode.None;
          });
  
          services.AddMvc()
          .AddRazorPagesOptions(options =&gt;
          {
               options.Conventions.AuthorizePage("/Contact");
          })
          .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
          #region snippet1   
          services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                  .AddCookie(options =&gt; options.ExpireTimeSpan = new System.TimeSpan(0, 10, 0));
          #endregion
  
          services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
  </code></pre>
  
  <p><strong>Configure</strong></p>
  
  <pre><code>        if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
              app.UseDatabaseErrorPage();
          }
          else
          {
              app.UseExceptionHandler("/Error");
              app.UseHsts();
          }
  
          app.UseHttpsRedirection();
          app.UseStaticFiles();
          app.UseCookiePolicy();
  
          // Call UseAuthentication before calling UseMVC.
          #region snippet2
          app.UseAuthentication();
          #endregion
  
          app.UseMvc();
  </code></pre>
  
  <p><strong>Authentication</strong></p>
  
  <pre><code>            #region snippet1
              var claims = new List&lt;Claim&gt;
              {
                  new Claim(ClaimTypes.Name, user.Email),
                  new Claim("FullName", user.FullName),
                  new Claim(ClaimTypes.Role, "Administrator"),
              };
  
              var claimsIdentity = new ClaimsIdentity(
                  claims, CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
  
              var authProperties = new AuthenticationProperties
              {
                  AllowRefresh = true,
                  // Refreshing the authentication session should be allowed.
  
                  ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                  // The time at which the authentication ticket expires. A 
                  // value set here overrides the ExpireTimeSpan option of 
                  // CookieAuthenticationOptions set with AddCookie.
  
                  IsPersistent = true,
                  // Whether the authentication session is persisted across 
                  // multiple requests. Required when setting the 
                  // ExpireTimeSpan option of CookieAuthenticationOptions 
                  // set with AddCookie. Also required when setting 
                  // ExpiresUtc.
  
                  //IssuedUtc = &lt;DateTimeOffset&gt;,
                  // The time at which the authentication ticket was issued.
  
                  //RedirectUri = &lt;string&gt;
                  // The full path or absolute URI to be used as an http 
                  // redirect response value.
              };
  
              await HttpContext.SignInAsync(
                  CookieAuthenticationDefaults.AuthenticationScheme, 
                  new ClaimsPrincipal(claimsIdentity),
                  authProperties);
              #endregion
  </code></pre>
  
  <p>and then I redirect to the contacts page but brought back to the login page. </p>