public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { log.LogInformation("ExternalLoginConfirmation called with returnurl " + returnUrl); if (User.IsSignedIn()) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new SiteUser { SiteGuid = Site.SiteGuid, SiteId = Site.SiteId, UserName = model.Email, Email = model.Email }; var result = await userManager.CreateAsync(user); if (result.Succeeded) { log.LogInformation("ExternalLoginConfirmation user created "); result = await userManager.AddLoginAsync(user, info); if (result.Succeeded) { log.LogInformation("ExternalLoginConfirmation AddLoginAsync succeeded "); await signInManager.SignInAsync(user, isPersistent : false); return(this.RedirectToLocal(returnUrl)); } else { log.LogInformation("ExternalLoginConfirmation AddLoginAsync failed "); } } else { log.LogInformation("ExternalLoginConfirmation failed to user created "); } AddErrors(result); } else { log.LogInformation("ExternalLoginConfirmation called with ModelStateInvalid "); } ViewData["ReturnUrl"] = returnUrl; return(View(model)); }
protected virtual async Task <SiteUser> CreateUserFromExternalLogin( ExternalLoginInfo externalLoginInfo, string providedEmail = null, bool?didAcceptTerms = null ) { var email = providedEmail; if (string.IsNullOrWhiteSpace(email)) { email = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email); if (string.IsNullOrWhiteSpace(email)) { email = externalLoginInfo.Principal.FindFirstValue("email"); } } DateTime?termsAcceptedDate = null; if (didAcceptTerms == true && !string.IsNullOrWhiteSpace(_userManager.Site.RegistrationAgreement)) { termsAcceptedDate = DateTime.UtcNow; } if (!string.IsNullOrWhiteSpace(email) && email.Contains("@")) { var userName = await _userManager.SuggestLoginNameFromEmail(_userManager.Site.Id, email); var newUser = new SiteUser { SiteId = _userManager.Site.Id, UserName = userName, Email = email, FirstName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.GivenName), LastName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Surname), AccountApproved = _userManager.Site.RequireApprovalBeforeLogin ? false : true, EmailConfirmed = _socialAuthEmailVerificationPolicy.HasVerifiedEmail(externalLoginInfo), AgreementAcceptedUtc = termsAcceptedDate, LastLoginUtc = DateTime.UtcNow }; //https://github.com/joeaudette/cloudscribe/issues/346 newUser.DisplayName = _displayNameResolver.ResolveDisplayName(newUser); var identityResult = await _userManager.CreateAsync(newUser); if (identityResult.Succeeded) { identityResult = await _userManager.AddLoginAsync(newUser, externalLoginInfo); return(newUser); } } return(null); }
// private ILogger log; private async Task <SiteUser> CreateUserFromExternalLogin( ExternalLoginInfo externalLoginInfo, string providedEmail = null, bool?didAcceptTerms = null ) { var email = providedEmail; if (string.IsNullOrWhiteSpace(email)) { email = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email); } DateTime?termsAcceptedDate = null; if (didAcceptTerms == true && !string.IsNullOrWhiteSpace(userManager.Site.RegistrationAgreement)) { termsAcceptedDate = DateTime.UtcNow; } if (!string.IsNullOrWhiteSpace(email) && email.Contains("@")) { var userName = await userManager.SuggestLoginNameFromEmail(userManager.Site.Id, email); var newUser = new SiteUser { SiteId = userManager.Site.Id, UserName = userName, Email = email, DisplayName = email.Substring(0, email.IndexOf("@")), FirstName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.GivenName), LastName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Surname), AccountApproved = userManager.Site.RequireApprovalBeforeLogin ? false : true, EmailConfirmed = socialAuthEmailVerificationPolicy.HasVerifiedEmail(externalLoginInfo), AgreementAcceptedUtc = termsAcceptedDate, LastLoginUtc = DateTime.UtcNow }; var identityResult = await userManager.CreateAsync(newUser); if (identityResult.Succeeded) { identityResult = await userManager.AddLoginAsync(newUser, externalLoginInfo); return(newUser); } } return(null); }
public async Task <IActionResult> LinkLoginCallback() { var user = await userManager.FindByIdAsync(HttpContext.User.GetUserId()); if (user == null) { return(View("Error")); } var info = await signInManager.GetExternalLoginInfoAsync(User.GetUserId()); if (info == null) { this.AlertDanger("oops something went wrong please try again"); return(RedirectToAction("ManageLogins")); } var result = await userManager.AddLoginAsync(user, info); if (!result.Succeeded) { this.AlertDanger("oops something went wrong, please try again"); } return(RedirectToAction("ManageLogins")); }
public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { log.LogDebug("ExternalLoginConfirmation called with returnurl " + returnUrl); //if (signInManager.IsSignedIn(User)) //{ // return RedirectToAction("Index", "Manage"); //} if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var userName = model.Email.Replace("@", string.Empty).Replace(".", string.Empty); var userNameAvailable = await userManager.LoginIsAvailable(Guid.Empty, userName); if (!userNameAvailable) { userName = model.Email; } var user = new SiteUser { SiteId = Site.Id, UserName = userName, Email = model.Email, AccountApproved = Site.RequireApprovalBeforeLogin ? false : true }; var result = await userManager.CreateAsync(user); if (result.Succeeded) { log.LogDebug("ExternalLoginConfirmation user created "); await ipAddressTracker.TackUserIpAddress(Site.Id, user.Id); result = await userManager.AddLoginAsync(user, info); if (result.Succeeded) { log.LogDebug("ExternalLoginConfirmation AddLoginAsync succeeded "); if (Site.RequireConfirmedEmail) // require email confirmation { var code = await userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action(new UrlActionContext { Action = "ConfirmEmail", Controller = "Account", Values = new { userId = user.Id.ToString(), code = code }, Protocol = HttpContext.Request.Scheme }); emailSender.SendAccountConfirmationEmailAsync( Site, model.Email, sr["Confirm your account"], callbackUrl).Forget(); // this is needed to clear the external cookie - wasn't needed in rc2 await signInManager.SignOutAsync(); if (this.SessionIsAvailable()) { this.AlertSuccess(sr["Please check your email inbox, we just sent you a link that you need to click to confirm your account"], true); return(Redirect("/")); } else { return(RedirectToAction("EmailConfirmationRequired", new { userId = user.Id, didSend = true })); } } else { if (Site.RequireApprovalBeforeLogin) { emailSender.AccountPendingApprovalAdminNotification(Site, user).Forget(); // this is needed to clear the external cookie - wasn't needed in rc2 await signInManager.SignOutAsync(); return(RedirectToAction("PendingApproval", new { userId = user.Id, didSend = true })); } else { await signInManager.SignInAsync(user, isPersistent : false); if (!string.IsNullOrEmpty(returnUrl)) { return(LocalRedirect(returnUrl)); } return(this.RedirectToSiteRoot(Site)); } } } else { log.LogDebug("ExternalLoginConfirmation AddLoginAsync failed "); } } else { log.LogDebug("ExternalLoginConfirmation failed to user created "); } AddErrors(result); } else { log.LogDebug("ExternalLoginConfirmation called with ModelStateInvalid "); model.RegistrationPreamble = Site.RegistrationPreamble; model.RegistrationAgreement = Site.RegistrationAgreement; model.AgreementRequired = Site.RegistrationAgreement.Length > 0; } ViewData["ReturnUrl"] = returnUrl; return(View(model)); }