【问题标题】:How can i implement forget password link in my project?如何在我的项目中实现忘记密码链接?
【发布时间】:2021-01-19 17:12:51
【问题描述】:
我在 asp.net core 2.1 mvc 中有一个现有项目。在登录页面,我添加了忘记密码链接,我想做这个东西;当用户单击此链接时,会出现其他页面然后用户输入自己的电子邮件地址并单击发送按钮然后密码重置链接将用户的电子邮件当用户单击密码重置链接时用户输入新密码并确认新密码。我怎样才能做到这一点。有很多资源,但一般在 .net 核心身份中,但我不使用身份。
【问题讨论】:
标签:
asp.net-core
asp.net-core-mvc
reset-password
【解决方案1】:
您可以使用 session 来重置密码。
烤面包
public IActionResult ForgetPassword()
{
//email: send a random code and save it in a session
HttpContext.Session.SetString("randomcode",new Random().Next(1000,10000).ToString());
return View();
}
[HttpPost]
public IActionResult ForgetPassword(string password,string randomCode)
{
//if the Vertification Code is not equals to the randomcode, return fail
if (randomCode != HttpContext.Session.GetString("randomcode"))
{
return View();
}
//update database
return View("index");
}
在此表格中输入新密码。
<form action="/home/ForgetPassword" method="post">
Password :<input type="text" name="password" value="" />
Confirm Password :<input type="text" name="confirm_password" value="" />
Vertification Code: <input type="text" name="randomCode" value="" />
<input type="submit" name="reset" value="reset"
onclick="Confirm(event)"/>
</form>
javascript
<script>
function Confirm(e) {
var passw = document.getElementsByName('password')[0].value
var confirmPassw = document.getElementsByName('confirm_password')[0].value
if (passw != confirmPassw)
e.preventDefault()
}
</script>