Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // get user object from the storage
- var user = await userManager.FindByIdAsync(userId);
- // change username and email
- user.Username = "NewUsername";
- user.Email = "New@email.com";
- // Persiste the changes
- await userManager.UpdateAsync(user);
- // generage email confirmation code
- var emailConfirmationCode = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
- // generate url for page where you can confirm the email
- var callbackurl= "http://example.com/ConfirmEmail";
- // append userId and confirmation code as parameters to the url
- callbackurl += String.Format("?userId={0}&code={1}", user.Id, HttpUtility.UrlEncode(emailConfirmationCode));
- var htmlContent = String.Format(
- @"Thank you for updating your email. Please confirm the email by clicking this link:
- <br><a href='{0}'>Confirm new email</a>",
- callbackurl);
- // send email to the user with the confirmation link
- await userManager.SendEmailAsync(user.Id, subject: "Email confirmation", body: htmlContent);
- // then this is the action to confirm the email on the user
- // link in the email should be pointing here
- public async Task<ActionResult> ConfirmEmail(string userId, string code)
- {
- var confirmResult = await userManager.ConfirmEmailAsync(userId, code);
- return RedirectToAction("Index");
- }
- public class ApplicationUser : IdentityUser
- {
- public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
- {
- // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
- var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
- // Add custom user claims here
- return userIdentity;
- }
- [MaxLength(256)]
- public string UnConfirmedEmail { get; set; }//this is what we add
- }
- //
- // POST: /Account/Login
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
- {
- if (!ModelState.IsValid)
- {
- return View(model);
- }
- var allowPassOnEmailVerfication = false;
- var user = await UserManager.FindByEmailAsync(model.Email);
- if (user != null)
- {
- if (!string.IsNullOrWhiteSpace(user.UnConfirmedEmail))
- {
- allowPassOnEmailVerfication = true;
- }
- }
- // This now counts login failures towards account lockout
- // To enable password failures to trigger account lockout, I changed to shouldLockout: true
- var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
- switch (result)
- {
- case SignInStatus.Success:
- return RedirectToLocal(returnUrl);
- case SignInStatus.LockedOut:
- return View("Lockout");
- case SignInStatus.RequiresVerification:
- return allowPassOnEmailVerfication ? RedirectToLocal(returnUrl) : RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
- case SignInStatus.Failure:
- default:
- ModelState.AddModelError("", "Invalid login attempt.");
- return View(model);
- }
- }
- public class IndexViewModel
- {
- public bool HasPassword { get; set; }
- public IList<UserLoginInfo> Logins { get; set; }
- public string PhoneNumber { get; set; }
- public bool TwoFactor { get; set; }
- public bool BrowserRemembered { get; set; }
- public string ConfirmedEmail { get; set; } //add this
- public string UnConfirmedEmail { get; set; } //and this
- }
- var userId = User.Identity.GetUserId();
- var currentUser = await UserManager.FindByIdAsync(userId);
- var unConfirmedEmail = "";
- if (!String.IsNullOrWhiteSpace(currentUser.UnConfirmedEmail))
- {
- unConfirmedEmail = currentUser.UnConfirmedEmail;
- }
- var model = new IndexViewModel
- {
- HasPassword = HasPassword(),
- PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
- TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
- Logins = await UserManager.GetLoginsAsync(userId),
- BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId),
- ConfirmedEmail = currentUser.Email,
- UnConfirmedEmail = unConfirmedEmail
- };
- <dt>Email:</dt>
- <dd>
- @Model.ConfirmedEmail
- @if (!String.IsNullOrWhiteSpace(Model.UnConfirmedEmail))
- {
- <em> - Unconfirmed: @Model.UnConfirmedEmail </em> @Html.ActionLink("Cancel", "CancelUnconfirmedEmail",new {email=Model.ConfirmedEmail})
- }
- else
- {
- @Html.ActionLink("Change Email", "ChangeEmail")
- }
- </dd>
- public class ChangeEmailViewModel
- {
- public string ConfirmedEmail { get; set; }
- [Required]
- [EmailAddress]
- [Display(Name = "Email")]
- [DataType(DataType.EmailAddress)]
- public string UnConfirmedEmail { get; set; }
- }
- public ActionResult ChangeEmail()
- {
- var user = UserManager.FindById(User.Identity.GetUserId());
- var model = new ChangeEmailViewModel()
- {
- ConfirmedEmail = user.Email
- };
- return View(model);
- }
- @model ProjectName.Models.ChangeEmailViewModel
- @{
- ViewBag.Title = "Change Email";
- }
- <h2>@ViewBag.Title.</h2>
- @using (Html.BeginForm("ChangeEmail", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
- {
- @Html.AntiForgeryToken()
- <h4>New Email Address:</h4>
- <hr />
- @Html.ValidationSummary("", new { @class = "text-danger" })
- @Html.HiddenFor(m=>m.ConfirmedEmail)
- <div class="form-group">
- @Html.LabelFor(m => m.UnConfirmedEmail, new { @class = "col-md-2 control-label" })
- <div class="col-md-10">
- @Html.TextBoxFor(m => m.UnConfirmedEmail, new { @class = "form-control" })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" class="btn btn-default" value="Email Link" />
- </div>
- </div>
- }
- [HttpPost]
- public async Task<ActionResult> ChangeEmail(ChangeEmailViewModel model)
- {
- if (!ModelState.IsValid)
- {
- return RedirectToAction("ChangeEmail", "Manage");
- }
- var user = await UserManager.FindByEmailAsync(model.ConfirmedEmail);
- var userId = user.Id;
- if (user != null)
- {
- //doing a quick swap so we can send the appropriate confirmation email
- user.UnConfirmedEmail = user.Email;
- user.Email = model.UnConfirmedEmail;
- user.EmailConfirmed = false;
- var result = await UserManager.UpdateAsync(user);
- if (result.Succeeded)
- {
- string callbackUrl =
- await SendEmailConfirmationTokenAsync(userId, "Confirm your new email");
- var tempUnconfirmed = user.Email;
- user.Email = user.UnConfirmedEmail;
- user.UnConfirmedEmail = tempUnconfirmed;
- result = await UserManager.UpdateAsync(user);
- callbackUrl = await SendEmailConfirmationWarningAsync(userId, "You email has been updated to: "+user.UnConfirmedEmail);
- }
- }
- return RedirectToAction("Index","Manage");
- }
- private async Task<string> SendEmailConfirmationWarningAsync(string userID, string subject)
- {
- string code = await UserManager.GenerateEmailConfirmationTokenAsync(userID);
- var callbackUrl = Url.Action("ConfirmEmail", "Account",
- new { userId = userID, code = code }, protocol: Request.Url.Scheme);
- await UserManager.SendEmailAsync(userID, subject,
- "Please confirm your account by clicking <a href="" + callbackUrl + "">here</a>");
- return callbackUrl;
- }
- public async Task<ActionResult> CancelUnconfirmedEmail(string emailOrUserId)
- {
- var user = await UserManager.FindByEmailAsync(emailOrUserId);
- if (user == null)
- {
- user = await UserManager.FindByIdAsync(emailOrUserId);
- if (user != null)
- {
- user.UnConfirmedEmail = "";
- user.EmailConfirmed = true;
- var result = await UserManager.UpdateAsync(user);
- }
- }
- else
- {
- user.UnConfirmedEmail = "";
- user.EmailConfirmed = true;
- var result = await UserManager.UpdateAsync(user);
- }
- return RedirectToAction("Index", "Manage");
- }
- var result = UserManager.ConfirmEmail(userId, code);
- if (result.Succeeded)
- {
- var user = UserManager.FindById(userId);
- if (!string.IsNullOrWhiteSpace(user.UnConfirmedEmail))
- {
- user.Email = user.UnConfirmedEmail;
- user.UserName = user.UnConfirmedEmail;
- user.UnConfirmedEmail = "";
- UserManager.Update(user);
- }
- }
Add Comment
Please, Sign In to add comment