Skip to content

Instantly share code, notes, and snippets.

@lnmunhoz
Last active August 29, 2015 14:08
Show Gist options
  • Save lnmunhoz/c6a5cc91fcb1ef6a051d to your computer and use it in GitHub Desktop.
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
[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