ExternalLoginConfirmation renvoie la valeur null après une connexion réussie avec facebook

L’implémentation de la connexion Facebook dans le modèle MVC 5 avait ajouté l’identifiant de l’application et le code secret.

Initialement, la connexion échouait car elle retournait null

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

Après la recherche, une solution a été trouvée pour remplacer la méthode ExternalLoginCallback existante par cette

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

Maintenant, ce problème est résolu, mais lorsque j’essaie d’associer votre compte Facebook. Vous avez authentifié avec succès avec Facebook. Veuillez entrer un nom d’utilisateur pour ce site ci-dessous et cliquez sur le bouton Enregistrer pour terminer la connexion.

 [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, ssortingng 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); } 

Lors du débogage, le nom d’utilisateur vient alors également ne peut pas comprendre pourquoi son exception de projection.

Je pense que lorsque GetExternalLoginInfoSync , il renvoie null.

J’ai eu le même problème et voici comment j’ai réussi à le réparer et à recevoir le courrier électronique de Facebook.

  • Mise à jour suite aux NuGet Pacakges
    • Microsoft.Owin à la version 3.1.0-rc1
    • Microsoft.Owin.Security à la version 3.1.0-rc1
    • Microsoft.Owin.Security.Cookies à la version 3.1.0-rc1
    • Microsoft.Owin.Security.OAuth à la version 3.1.0-rc1
    • Microsoft.Owin.Security.Facebook à la version 3.1.0-rc1

Ajoutez ensuite le code suivant à la classe de 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); 

Voici la classe de définition de 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); } } 

juste mettre à jour

Microsoft.Owin.Security.Facebook 3.0.1 à

Microsoft.Owin.Security.Facebook 3.1.0

travaillé pour moi .. mais petit problème vous ne recevrez pas de courrier électronique sur la page suivante après la connexion réussie. c’était bien pour moi.

Peut-être que cela peut aider quelqu’un:

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

Je reçois tout, Nom et Email