facebook登录成功后,ExternalLoginConfirmation返回null

在MVC 5模板中实现Facebook登录,添加了应用程序ID和密码。

最初登录失败,因为它返回null

public async Task ExternalLoginCallback(string returnUrl) { // Crashes on this line var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return RedirectToAction("Login"); } } 

在搜索之后遇到了一个解决方案,该解决方案用这个替换现有的ExternalLoginCallback方法

 [AllowAnonymous] public async Task ExternalLoginCallback(string returnUrl) { var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie); if (result == null || result.Identity == null) { return RedirectToAction("Login"); } var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier); if (idClaim == null) { return RedirectToAction("Login"); } var login = new UserLoginInfo(idClaim.Issuer, idClaim.Value); var name = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", ""); // Sign in the user with this external login provider if the user already has a login var user = await UserManager.FindAsync(login); if (user != null) { await SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } else { // If the user does not have an account, then prompt the user to create an account ViewBag.ReturnUrl = returnUrl; ViewBag.LoginProvider = login.LoginProvider; return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { UserName = name }); } } 

现在该问题已经解决,但是当我尝试关联您的Facebook帐户时。 您已成功通过Facebookvalidation。 请在下面输入此站点的用户名,然后单击“注册”按钮完成登录。

 [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Here comes the error System.NullReferenceException: Object reference not set to an instance of an object. var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser() { UserName = model.UserName }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); } 

在调试用户名时,也无法弄明白为什么它会抛出exception。

我认为当它到达返回null的GetExternalLoginInfoSync时。

我有同样的问题,这就是我如何设法修复它并从Facebook获取电子邮件。

  • 更新NuGet Pacakges之后
    • Microsoft.Owin到版本3.1.0-rc1
    • Microsoft.Owin.Security到版本3.1.0-rc1
    • Microsoft.Owin.Security.Cookies到版本3.1.0-rc1
    • Microsoft.Owin.Security.OAuth到版本3.1.0-rc1
    • Microsoft.Owin.Security.Facebook3.1.0-rc1

然后将以下代码添加到Identity Startup

 var facebookOptions = new FacebookAuthenticationOptions() { AppId = "your app id", AppSecret = "your app secret", BackchannelHttpHandler = new FacebookBackChannelHandler(), UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name", Scope = { "email" } }; app.UseFacebookAuthentication(facebookOptions); 

这是FacebookBackChannelHandler()的定义类:

 using System; using System.Net.Http; public class FacebookBackChannelHandler : HttpClientHandler { protected override async System.Threading.Tasks.Task SendAsync( HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { // Replace the RequestUri so it's not malformed if (!request.RequestUri.AbsolutePath.Contains("/oauth")) { request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token")); } return await base.SendAsync(request, cancellationToken); } } 

只是更新

Microsoft.Owin.Security.Facebook 3.0.1 to

Microsoft.Owin.Security.Facebook 3.1.0

为我工作..但小问题,你成功登录后不会在下一页收到电子邮件。 这对我来说很好。

可能它可以帮助一些人:

 var option = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions(); option.AppId = ConfigurationManager.AppSettings.Get("fbAppId"); option.AppSecret = ConfigurationManager.AppSettings.Get("fbAppSecret"); option.Scope.Add("email"); option.UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=name,email"; app.UseFacebookAuthentication(option); 

我得到了一切,名字和电子邮件