无法登录facebook登录MVC5。它始终在GetExternalLoginInfoAsync()上返回NULL

时间:2014-10-24 14:35:38

标签: asp.net-mvc facebook visual-studio-2013 owin

我整晚都在摸着头,用谷歌搜索,但似乎没有什么对我有用。当尝试使用Facebook登录我的mvc5应用程序时,我继续在AuthenticationManager.GetExternalLoginInfoAsync()中获取空引用错误。使用Google登录效果非常好。

这是我的设置:

  1. 网站正在IIS Express上运行(也尝试过本地IIS)。
  2. 已经尝试了http和https,因为有些文章说fb需要SSL。所以我现在坚持使用https://localhost:44302/(注意:Google无论如何都能正常工作)。
  3. 注意:我使用的是mvc5中的默认设置/模板,没有更改任何内容 - 当然除了用于fb的AppId和AppSecret。

    任何人都可以投入一些解决方案。感谢。

1 个答案:

答案 0 :(得分:1)

替换var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

通过此代码:

var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);
if (result == null || result.Identity == null)//here will check if user login done
{
    return RedirectToAction("Login");
}

var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);
if (idClaim == null)
{
    return RedirectToAction("Login");
}

var login = new UserLoginInfo(idClaim.Issuer, idClaim.Value);//here getting login info
var name = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", "");//here getting user name
相关问题