2016-11-11 82 views
1

在MVC 5模板中實現Facebook登錄,添加了應用程序ID和密碼。在Facebook成功登錄後ExternalLoginConfirmation返回null

最初登錄爲失敗的,因爲它是返回null

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

跨越它說這個

[AllowAnonymous] 
    public async Task<ActionResult> 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 }); 
     } 
    } 

現在的問題,以取代現有的ExternalLoginCallback方法解決搜索出來之後就解決了,但是當我嘗試關聯您的Facebook帳戶。 您已成功通過Facebook進行身份驗證。這個網站請在下面輸入用戶名,然後點擊註冊按鈕完成登錄。

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> 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); 
    } 

在調試的用戶名來,然後也弄不明白的是,爲什麼它投擲例外。

+1

我有同樣的問題,你得到它的工作? – crystyxn

+0

不幸的是沒有。 – Dave

回答

2

我認爲當它得到GetExternalLoginInfoSync,返回null。

我有同樣的問題,這裏是我如何解決它,並從Facebook獲得電子郵件。

  • 更新以下的NuGet Pacakges
    • Microsoft.Owin3.1.0-rc1版本
    • Microsoft.Owin.Security3.1.0-rc1版本
    • Microsoft.Owin.Security.Cookies3.1.0-rc1版本
    • Microsoft.Owin.Security.OAuth到版本3.1.0-rc1
    • Microsoft.Owin.Security.Facebook到版本3.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<HttpResponseMessage> 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); 
    } 
} 
+0

爲我做了這份工作,也得到了用戶的電子郵件 – ThunderDev

2

剛剛更新

Microsoft.Owin.Security.Facebook 3.0.1

Microsoft.Owin.Security.Facebook 3.1.0

工作對我來說..但小問題,你不會成功登錄後在下一頁獲取電子郵件。這對我來說很好。

+0

這對我來說是正確的答案。謝謝! –

+0

我已經有了這個 - 我創建了一個帶有身份的新MVC項目 - 並且發生這種情況。 – niico

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

我得到的一切,名稱以及電子郵件