AuthenticationManager.GetExternalLoginInfoAsync() 始终返回 null

3
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

为什么这行代码总是返回 null?无论用户使用哪个提供程序进行登录都会出现此问题。我知道流程是正确的,因为在本地可以正常工作,但是当我部署网站时它总是返回 null。
据我所了解,它使用外部 cookie 跟踪此问题,我可以确定不是浏览器的问题,因为在同一浏览器上本地可以正常工作。
即使我已经仔细检查了所有提供程序的控制面板中的域名是否“允许”,但这种情况仍会发生。就像我说的,一切在本地都能正常工作。
我尝试过重新启动 IIS,正如一些答案建议的那样。
还有这些行在 Startup.Auth.cs 中,cookie 域名设置正确。
 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个回答

1

确保在部署时不要覆盖你的(Facebook、Twitter等)提供者设置。

同时,确保你的回调URL已经正确地在提供者处注册。我正在使用LinkedIn,它需要你的回调URL是完整的,例如:https://domain.com/signin-linkedin

我有以下设置,可以正常工作且没有任何问题:

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);

是的,我已经注册了/signin-linkedin和相关的回调URI,但我不确定路径甚至来自哪里...我的请求中的redirect_uri指向一个HTML页面(/auth-complete.html)。我不确定redirect_uri和callback url是否在这里引用相同的内容,但我尝试将http://example.com/auth-complete.html添加为重定向URI,但没有解决问题。我也不确定您所说的覆盖提供程序设置是什么意思?它可能在哪里被覆盖。 - parliament
还尝试使用CookieSecure + CookieName而不是像您一样使用CookieDomain,但没有帮助。 - parliament

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接