Exemplo n.º 1
0
 public IActionResult ChangeEmail(ChangeEmailVM vm)
 {
     if (ModelState.IsValid)
     {
         var user = _userService.GetUserByUserName(User.Identity.Name);
         if (user.Email != vm.OldEmail)
         {
             ModelState.AddModelError("OldEmail", "Your current email is not correct.");
             return(View(vm));
         }
         string code = Guid.NewGuid().ToString();
         user.EmailCode = code;
         user.NewEmail  = vm.NewEmail;
         _userService.EditUser(user);
         var subject      = "Your Email is being changed!";
         var CallBackUrl  = Url.Action("ConfirmChangeEmail", "User", new { userId = _protector.Encode(user.Id.ToString()), code = _protector.Encode(code) }, protocol: Request.Scheme);
         var mailTemplate = System.IO.File.ReadAllText("./Models/ViewModel/UserVM/EmailConfirm.html");
         mailTemplate = mailTemplate.Replace("{UserName}", user.UserName);
         mailTemplate = mailTemplate.Replace("{Content}", "Please click bellow button for confirm your changing email process.");
         mailTemplate = mailTemplate.Replace("{ConfirmationLink}", CallBackUrl);
         _emailService.SendEmail(user.Email, subject, mailTemplate);
         ModelState.AddModelError("", "Change email In process. Please Check email to confirm.");
         return(View(vm));
     }
     return(View(vm));
 }
Exemplo n.º 2
0
 public ActionResult ChangeEmail(ChangeEmailVM model)
 {
     if (ModelState.IsValid)
     {
         string res = _accountService.ChangeEmail(model.Email, User.Identity.GetUserId(), model.Password);
         if (res.Length == 0)
         {
             return(RedirectToAction("Logout"));
         }
         else
         {
             ModelState.AddModelError("", res);
         }
     }
     return(View(model));
 }
Exemplo n.º 3
0
        public async Task <IdentityResult> ChangeEmail(ChangeEmailVM email, System.Security.Claims.ClaimsPrincipal user)
        {
            string userId = user.Claims.First(c => c.Type == "UserID").Value;

            var usermodel = await _userManager.FindByIdAsync(userId);

            try
            {
                usermodel.Email           = email.NewEmail;
                usermodel.NormalizedEmail = email.NewEmail.Normalize();
                var result = await _userManager.UpdateAsync(usermodel);

                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> ChangeEmail(ChangeEmailVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            //TODO: chyba inaczej trzeba sprawdzać czy email jest zajęty
            var user1 = await _userManager.FindByEmailAsync(model.NewEmail);

            if (user1 != null)
            {
                ModelState.AddModelError(nameof(model.NewEmail), "Podany adres jest już używany");
                return(View());
            }

            var user = await _userManager.GetUserAsync(HttpContext.User);

            var correctPassword = await _userManager.CheckPasswordAsync(user, model.Password);

            if (correctPassword == false)
            {
                ModelState.AddModelError(nameof(model.Password), "Nieprawidłowe hasło");
                return(View());
            }

            var tokeN = await _userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail);

            var link = Url.Action("AcceptChangeEmail", "Account", new { userId = user.Id, token = tokeN, newEmail = model.NewEmail }, protocol: HttpContext.Request.Scheme);
            var res  = await _emailSender.SendEmailAsync(model.NewEmail, link, "Confirm Change Email");

            if (res)
            {
                TempData["Result"] = "Wysłano wiadomość z linkiem potwierdzającym zmiane emaila";
                return(RedirectToAction("ResultAction", "Account", new { area = "Doctor" }));
            }
            else
            {
                return(View());
            }
        }
Exemplo n.º 5
0
 public async Task <object> ChangeEmail([FromBody] ChangeEmailVM changeEmailVM)
 => await _userService.ChangeEmail(changeEmailVM, User);