title: Invalid state cookie. An error was encountered while handling the remote login. ASP.NET Core MVC external social login
body: <p>While implementing external social login in ASP.NET Core2.2 MVC web application without ASP.NET Core Identity. I am getting below error while redirecting back to the application after successful sign in with Google, Facebook, Twitter, LinkedIn and Microsoft.</p>
  
  <blockquote>
    <p>An unhandled exception occurred while processing the request.
    Exception: Invalid state cookie. Unknown location</p>
    
    <p>Exception: An error was encountered while handling the remote login.
    Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/FYQgr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FYQgr.png" alt="enter image description here"></a></p>
  
  <p>Following are the settings in Startup.cs file</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
     {
         services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
         {
             // This lambda determines whether user consent for non-essential cookies is needed for a given request.
             options.CheckConsentNeeded = context =&gt; true;
             options.MinimumSameSitePolicy = SameSiteMode.None;
         });
  
         services
             .AddAuthentication(options =&gt;
             {
                 options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                 options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
             })
             .AddCookie(options =&gt;
             {
                 options.Cookie.IsEssential = true;
             })
             .AddGoogle(options =&gt;
             {
                 options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                 options.ClientId = Configuration["Authentication:Google:ClientId"];
                 options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                 options.CallbackPath = "/externallogincallback";             
             })
             .AddFacebook(facebookOptions =&gt;
             {
                 facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
                 facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
                 facebookOptions.CallbackPath = "/externallogincallback";                                        
             })
             .AddLinkedIn(linkedinOptions =&gt;
             {
                 linkedinOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                 linkedinOptions.ClientId = Configuration["Authentication:LinkedIn:ClientId"];
                 linkedinOptions.ClientSecret = Configuration["Authentication:LinkedIn:ClientSecret"];
                 linkedinOptions.CallbackPath = "/externallogincallback";                    
             })
             .AddTwitter(twitterOptions =&gt;
             {
                 twitterOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                 twitterOptions.ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"];
                 twitterOptions.ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"];
                 twitterOptions.CallbackPath = "/Home/externallogincallback";                    
             }).AddMicrosoftAccount(microsoftOptions =&gt;
             {
                 microsoftOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                 microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ClientId"];
                 microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
                 microsoftOptions.CallbackPath = "/externallogincallback";
             });
         services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
     }
  </code></pre>
  
  <p>Following are the details of HomeController.cs (As I am not using Identity so I need to define redirect url specifically.)</p>
  
  <pre class="lang-cs prettyprint-override"><code>      //Action to issue a challange to google login
          public IActionResult LogInMicrosoft(string provider)
          {
              //provider = Microsot or Google or LinkedIn or Twitter or Facebook
              provider = "Microsoft";
              //Issue a challenge to external login middleware to trigger sign in process
              //return new ChallengeResult(provider);
  
              var authenticationProperties = new AuthenticationProperties
              {
                  RedirectUri = Url.Action("externallogincallback")
              };
  
              return Challenge(authenticationProperties, provider);
              //return new ChallengeResult(provider);
          }
  
          //Callback action to retrive signin user details
          //public IActionResult externallogincallback(string returnUrl = null, string remoteError = null)\
          public IActionResult externallogincallback()
          {
              //Here we can retrieve the claims
              // read external identity from the temporary cookie
              //var authenticateResult = HttpContext.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie");
              var result = HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
  
              if (result.Result?.Succeeded != true)
              {
                  throw new Exception("External authentication error");
              }
  
              // retrieve claims of the external user
              var externalUser = result.Result.Principal;
              if (externalUser == null)
              {
                  throw new Exception("External authentication error");
              }
  
              // retrieve claims of the external user
              var claims = externalUser.Claims.ToList();
  
              // try to determine the unique id of the external user - the most common claim type for that are the sub claim and the NameIdentifier
              // depending on the external provider, some other claim type might be used
              //var userIdClaim = claims.FirstOrDefault(x =&gt; x.Type == JwtClaimTypes.Subject);
              var userIdClaim = claims.FirstOrDefault(x =&gt; x.Type == ClaimTypes.NameIdentifier);
              if (userIdClaim == null)
              {
                  throw new Exception("Unknown userid");
              }
  
              var externalUserId = userIdClaim.Value;
              var externalProvider = userIdClaim.Issuer;
  
              // use externalProvider and externalUserId to find your user, or provision a new user
  
              return RedirectToAction("Privacy", "Home");
          }
  </code></pre>