AuthenticationManager.GetExternalLoginInfoAsync() always null

1.6k Views Asked by At
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

Why must this line always return null? It happens when the user tries to login using ANY provider. I know the FLOW is correct because actually it works locally, but when I deploy the website it always returns null.

From what I understand it's using an external cookie to track this and I can be sure it's not the browser because it works locally on the same browser.

This happens for ALL providers, even though I've double checked that all of them have the domain as "allowed" in each respective control panel. Like I said everything works locally.

I've tried restarting IIS as some answers suggested.

Also have these lines in Startup.Auth.cs with the cookie domian correctly set

 app.UseCookieAuthentication(new CookieAuthenticationOptions
  {
       CookieDomain = ".example.com",
            ...
  });

  //use a cookie to temporarily store information about a user logging in with a third party login provider
  app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
1

There are 1 best solutions below

2
On BEST ANSWER

Make sure your (facebook, twitter etc.) provider settings are not overriden on deployment.

Also make sure that your callback url is correctly registered at the provider. I'm using LinkedIn which needs a full url to your callback, eg. https://domain.com/signin-linkedin

I have the following setup, which works without any problems:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    CookieSecure = CookieSecureOption.SameAsRequest,
    CookieName = "CustomCookieName",
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => Some.Custom.Identity.Helper.GenerateUserIdentityHelperAsync(user, manager)) // This helper method returns ClaimsIdentity for the user
    }
});

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);