Last active
August 29, 2015 14:08
-
-
Save lnmunhoz/c6a5cc91fcb1ef6a051d to your computer and use it in GitHub Desktop.
This is for fix the facebook authentication errors that comes with erros in ASP.NET default template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) | |
{ | |
if (User.Identity.IsAuthenticated) | |
{ | |
return RedirectToAction("Manage"); | |
} | |
if (ModelState.IsValid) | |
{ | |
// Get the information about the user from the external login provider | |
var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie); | |
// Here will check if user login done | |
if (result == null || result.Identity == null) | |
{ | |
return RedirectToAction("Login"); | |
} | |
var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier); | |
if (idClaim == null) | |
{ | |
return RedirectToAction("Login"); | |
} | |
// Here getting login info | |
var login = new UserLoginInfo(idClaim.Issuer, idClaim.Value); | |
var user = new ApplicationUser() { UserName = model.UserName }; | |
var resultUser = await UserManager.CreateAsync(user); | |
if (resultUser.Succeeded) | |
{ | |
resultUser = await UserManager.AddLoginAsync(user.Id, login); | |
if (resultUser.Succeeded) | |
{ | |
await SignInAsync(user, isPersistent: false); | |
return RedirectToLocal(returnUrl); | |
} | |
} | |
AddErrors(resultUser); | |
} | |
ViewBag.ReturnUrl = returnUrl; | |
return View(model); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment