public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model) { if (User.Identity.IsAuthenticated) { return RedirectToLocal(model.ReturnUrl); } // Get the information about the user from the external login provider var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return View("ExternalLoginFailure"); } ApplicationUser existingUser = await UserManager.FindByEmailAsync(loginInfo.Email); if (existingUser != null) { var existingLogins = await UserManager.GetLoginsAsync(existingUser.Id); // Another user with the same email exists ViewBag.LoginProvider = loginInfo.Login.LoginProvider; return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel() { ReturnUrl = model.ReturnUrl, Email = loginInfo.Email, ExistingLogins = existingLogins }); } var user = new ApplicationUser { UserName = loginInfo.DefaultUserName, Email = loginInfo.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // This must be done for claim changes to be included in the current app cookie? Why MVC... var authenticationContext = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie); if (authenticationContext != null) { AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant( await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie), authenticationContext.Properties); } return RedirectToLocal(model.ReturnUrl); } } AddErrors(result); // Some user creation error return View("ExternalLoginFailure"); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }