예제 #1
0
        public async Task<IActionResult> ChangeEmail(ChangeEmailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = GetCurrentUser();
            if (user != null)
            {
                if(!await _userManager.CheckPasswordAsync(user, model.Password))
                {
                    ModelState.AddModelError("Password", "The password supplied is not correct");
                    return View(model);
                }

                var existingUser = await _userManager.FindByEmailAsync(model.NewEmail.Normalize());
                if(existingUser != null)
                {
                    // The username/email is already registered
                    ModelState.AddModelError("NewEmail", "The email supplied is already registered");
                    return View(model);
                }

                user.PendingNewEmail = model.NewEmail;
                await _userManager.UpdateAsync(user);

                var token = await _userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail);
                var callbackUrl = Url.Action("ConfirmNewEmail", "Manage", new { token = token }, protocol: HttpContext.Request.Scheme);
                await _emailSender.SendEmailAsync(user.Email, "Confirm your allReady account",
                    "Please confirm your new email address for your allReady account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>. Note that once confirmed your original email address will cease to be valid as your username.");

                return RedirectToAction(nameof(EmailConfirmationSent));                
            }

            return RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error });
        }
예제 #2
0
        public async Task<IActionResult> ChangeEmail(ChangeEmailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = GetCurrentUser();
            if (user != null)
            {
                if(!await _userManager.CheckPasswordAsync(user, model.Password))
                {
                    ModelState.AddModelError(nameof(model.Password), "The password supplied is not correct");
                    return View(model);
                }

                var existingUser = await _userManager.FindByEmailAsync(model.NewEmail.Normalize());
                if(existingUser != null)
                {
                    // The username/email is already registered
                    ModelState.AddModelError(nameof(model.NewEmail), "The email supplied is already registered");
                    return View(model);
                }

                user.PendingNewEmail = model.NewEmail;
                await _userManager.UpdateAsync(user);

                await BuildCallbackUrlAndSendNewEmailAddressConfirmationEmail(user, model.NewEmail);

                return RedirectToAction(nameof(EmailConfirmationSent));                
            }

            return RedirectToAction(nameof(Microsoft.Data.Entity.Metadata.Internal.Index), new { Message = ManageMessageId.Error });
        }
예제 #3
0
        public async Task<IActionResult> ChangeEmail(ChangeEmailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = GetCurrentUser();
            if (user != null)
            {
                if(!await _userManager.CheckPasswordAsync(user, model.Password))
                {
                    ModelState.AddModelError(nameof(model.Password), PASSWORD_INCORRECT);
                    return View(model);
                }

                var existingUser = await _userManager.FindByEmailAsync(model.NewEmail.Normalize());
                if(existingUser != null)
                {
                    // The username/email is already registered
                    ModelState.AddModelError(nameof(model.NewEmail), EMAIL_ALREADY_REGISTERED);
                    return View(model);
                }

                user.PendingNewEmail = model.NewEmail;
                await _userManager.UpdateAsync(user);

                var token = await _userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail);
                //var callbackUrl = Url.Action(nameof(ConfirmNewEmail), MANAGE_CONTROLLER, new { token = token }, protocol: HttpContext.Request.Scheme);
                var callbackUrl = Url.Action(new UrlActionContext { Action = nameof(ConfirmNewEmail), Controller = MANAGE_CONTROLLER, Values = new { token = token },
                    Protocol = HttpContext.Request.Scheme });
                await _emailSender.SendEmailAsync(user.Email, EMAIL_CONFIRMATION_SUBJECT, string.Format(NEW_EMAIL_CONFIRM, callbackUrl));

                return RedirectToAction(nameof(EmailConfirmationSent));                
            }

            return RedirectToAction(nameof(Microsoft.Data.Entity.Metadata.Internal.Index), new { Message = ManageMessageId.Error });
        }