0

I am sending the e-mail replacement token that I created with identity as an e-mail. However, I could not understand whether the token in the incoming link is correct or not.

The controller where I bought the token

 [Route("ChangeEmailToken")]
        [HttpGet]

         public async Task<IActionResult> ChangeEmailToken([FromQuery] string token, [FromQuery] string oldEmail, [FromQuery] string newEmail)

        {
            System.Console.WriteLine("Başladı");

            var user = await _userManager.FindByEmailAsync(oldEmail);
            if (user != null && oldEmail == user.Email)
            {
                System.Console.WriteLine(await _userManager.VerifyUserTokenAsync(user,_userManager.Options.Tokens.ChangeEmailTokenProvider, "ChangeEmail", token));
               
              var result = await _userManager.SetEmailAsync(user,newEmail);
                return View();
            }


            return RedirectToAction("Index", "zfc");

        }

The controller I sent the token to

[HttpPost]
        [Route("resetEmailAdress")]
        public async Task<IActionResult> resetEmailAdress(ResetAdminPasswordModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData["resetEmail"] = "İşlem Başarısız!";
                return RedirectToAction("AdminSettings", "zfc");
            }

            if (model.mEmail == null || model.musername == null || model.mpassword == null || model.newmEmail == null)
            {
                TempData["resetEmail"] = "Lütfen Tüm Bilgileri Eksiksiz Doldurun!";
                return RedirectToAction("AdminSettings", "zfc");
            }

            var user = await _userManager.FindByEmailAsync(model.mEmail);

            if (user == null)
            {
                TempData["resetEmail"] = "Böyle bir kullanıcı bulunamadı!";
                return RedirectToAction("AdminSettings", "zfc");
            }
            if (model.musername != user.UserName)
            {
                TempData["resetEmail"] = "Kullanıcı adınız hatalı!!";
                return RedirectToAction("AdminSettings", "zfc");
            }

            if (!await _userManager.CheckPasswordAsync(user, model.mpassword))
            {
                TempData["resetEmail"] = "Lütfen şifrenizi doğru girin.";

                return RedirectToAction("AdminSettings", "zfc");
            }


            var token = await _userManager.GenerateChangeEmailTokenAsync(user, model.mEmail);

            var resetLink = Url.Action("ChangeEmailToken", "login", new { token = token, oldEmail = user.Email, newEmail = model.newmEmail }, protocol: HttpContext.Request.Scheme);
         System.Console.WriteLine(token);
            var mail = @"
             <!DOCTYPE html><html lang='tr'><head> <meta charset='UTF-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>ZFC API RESET PASSWORD</title> <link rel='preconnect' href='https://fonts.googleapis.com'> <link rel='preconnect' href='https://fonts.gstatic.com' crossorigin> <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap' rel='stylesheet'> <style>*{-webkit-tap-highlight-color: transparent;margin:0;padding:0;box-sizing:border-box}body,html{width:100%;font-family:Roboto,sans-serif;height:100%}html{font-size:10px;background-color:#e5eff4}body{font-size:1.6rem}.zfc-login{width:100%;height:100%;display:flex;justify-content:center;align-items:center}.zfc-login-card{width:100%;max-width:500px;padding:1.5rem}.login-head{background-color:#212121;color:#fff;padding:1rem;padding-left:2rem;padding-right:1.5rem;border-top-left-radius:1rem;border-top-right-radius:1rem;display:flex;justify-content:space-between;align-items:center}.login-body{background-color:#fff;border-bottom-left-radius:1rem;border-bottom-right-radius:1rem;padding:1rem;padding-top:2rem}.zfc-form-item{margin-bottom:1rem;display:flex;flex-direction:column;width:90%;margin-left:5%}.zfc-form-item input{margin-top:.5rem;padding:1rem;outline:0;font-size:1.8rem;border-radius:1rem;border:none;border:1px solid #a5a3a3}.submit-btn{margin-right:5%;padding:1rem 2rem;font-size:1.6rem;font-weight:700;background-color:#212121;color:#fff;outline:0;border:none;border:1px solid #212121;cursor:pointer;border-radius:80px;display:block;transition:.4s;margin-bottom:1rem}.submit-btn:hover{letter-spacing: 3px;background-color:#fff;color:#212121}.redirect{color:#fff;text-decoration:none;transition:.4s}.redirect:hover{letter-spacing:2px}p{margin-bottom: 1rem;}</style></head><body> <div class='zfc-login'> <div class='zfc-login-card'> <div class='login-head'> <a href='https://localhost:5001/login/api' class='redirect'>Giriş Yap ></a> <span></span> </div><div class='login-body'> <p>Merhaba ";
            var tx2 = $"{user.UserName},</p><p>E mail adresini aşağıdaki butonu kullanarak değiştirebilirsin.</p><center> <a href='{resetLink}' style='text-decoration:none' class='submit-btn'>Sıfırla</a> </center> </div></div></div></body></html>";




            await _emailSender.SendEmailAsync(user.Email, "E postanızı Değiştirin!", mail + tx2);

            await _signInManager.SignOutAsync();

            return RedirectToAction("Index", "zfc");
        }

With GenerateChangeEmailTokenAsync I receive the token I send without any problems. However System.Console.WriteLine(await _userManager.VerifyUserTokenAsync(user,_userManager.Options.Tokens.ChangeEmailTokenProvider, "ChangeEmail", token));

It always returns false value.

enter image description here

warn: Microsoft.AspNetCore.Identity.UserManager[9]
      VerifyUserTokenAsync() failed with purpose: ChangeEmail for user 1f14e476-5bca-4c69-b295-ea09ba2e1469.

I am getting the warning.

https://stackoverflow.com/a/43324580/15929287

I also tried the method but it does not change the email address. How can I check whether the Email reset token I created and the incoming token match?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You try to solve it by decoding the URL-encoded token:var decodedTokenString = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(token)); – Tupac Dec 06 '21 at 08:38
  • I tried that way too. It came up when I searched for the error. I even printed the sent token and the received token to the console, but there was no change. Since I couldn't find any solution, I removed the e-mail replacement system from my project. I would be very happy if you can share a solution. – ZFC AKADEMİ Dec 06 '21 at 14:05

0 Answers0