OTP 过期检查 ASP.NET Core MVC
OTP Expiration Check ASP.NET Core MVC
我正在构建一个 ASP.NET 核心 MVC 应用程序,我在其中将 OTP 发送给用户,用户必须在 30 秒内输入 OTP 才能使 OTP 工作。如何检查用户在输入字段中输入的 OTP 是否在生成 OTP 后的 30 秒内输入?
我已经编写了用于生成、获取和提交 OTP 的控制器。只需要知道OTP Generation的时间校验逻辑即可。
控制器
[HttpGet]
public IActionResult GenerateOtp()
{
return View();
}
[HttpPost]
public IActionResult SendOtp()
{
string num = "01223456789";
int len = num.Length;
string otp = string.Empty;
int otpDigit = 4;
string finalDigit;
int getIndex;
for (int i = 0; i < otpDigit; i++) {
do
{
getIndex = new Random().Next(0, len);
finalDigit = num.ToCharArray()[getIndex].ToString();
} while (otp.IndexOf(finalDigit) != -1 );
otp += finalDigit;
}
TempData["otp"] = otp;
return RedirectToAction("GenerateOtp", "Home");
}
[HttpPost]
public IActionResult SubmitOtp([FromForm] int finalDigit, int sentotp)
{
if (finalDigit == null)
return NoContent();
else if (finalDigit == sentotp && ***30 Second Check Here***)
{
return Ok("Activated Successfully");
}
else if(!(***30 Second Check Here***))
{
return BadRequest("OTP Timedout");
}
else
{
return BadRequest("Please Enter Valid OTP");
}
}
}
}
**View**
@{
ViewData["Title"] = "GenerateOtp";
}
<h1>GenerateOtp</h1>
<form method="post" asp-action="SendOtp" asp-controller="Home">
<br />
<input type="submit" value="GetOtp" class="btn btn-primary btn-lg"/>
<br />
<div>
@TempData["otp"]
</div>
<br />
<input type="number"/>
<br />
<input type="submit" value="SubmitOtp" class="btn btn-primary btn-lg"/>
</form>
与保存到临时数据中的OTP相同,您可以在SendOtp Action方法中保存发送OTP时的时间戳。在 SubmitOtp 操作方法中,从 TempData 中读取该时间戳。如果当前时间戳和 TempData 时间戳之间的差异超过 30 秒,则拒绝请求。
public IActionResult GenerateOtp()
{
return View();
}
[HttpPost]
public IActionResult SendOtp()
{
string num = "01223456789";
int len = num.Length;
string otp = string.Empty;
int otpDigit = 4;
string finalDigit;
int getIndex;
for (int i = 0; i < otpDigit; i++) {
do
{
getIndex = new Random().Next(0, len);
finalDigit = num.ToCharArray()[getIndex].ToString();
} while (otp.IndexOf(finalDigit) != -1 );
otp += finalDigit;
}
TempData["otp"] = otp;
TempData["timestamp"] = DateTime.Now;
return RedirectToAction("GenerateOtp", "Home");
}
[HttpPost]
public IActionResult SubmitOtp([FromForm] int finalDigit, int sentotp)
{
if (finalDigit == null)
return NoContent();
else if (finalDigit == sentotp && **30 Second Check Here**)
{
return Ok("Activated Successfully");
}
else if((DateTime.Now - Convert.DateTime(TempData["timestamp"])).TotalSeconds > 30)
{
return BadRequest("OTP Timedout");
}
else
{
return BadRequest("Please Enter Valid OTP");
}
}
我正在构建一个 ASP.NET 核心 MVC 应用程序,我在其中将 OTP 发送给用户,用户必须在 30 秒内输入 OTP 才能使 OTP 工作。如何检查用户在输入字段中输入的 OTP 是否在生成 OTP 后的 30 秒内输入?
我已经编写了用于生成、获取和提交 OTP 的控制器。只需要知道OTP Generation的时间校验逻辑即可。
控制器
[HttpGet]
public IActionResult GenerateOtp()
{
return View();
}
[HttpPost]
public IActionResult SendOtp()
{
string num = "01223456789";
int len = num.Length;
string otp = string.Empty;
int otpDigit = 4;
string finalDigit;
int getIndex;
for (int i = 0; i < otpDigit; i++) {
do
{
getIndex = new Random().Next(0, len);
finalDigit = num.ToCharArray()[getIndex].ToString();
} while (otp.IndexOf(finalDigit) != -1 );
otp += finalDigit;
}
TempData["otp"] = otp;
return RedirectToAction("GenerateOtp", "Home");
}
[HttpPost]
public IActionResult SubmitOtp([FromForm] int finalDigit, int sentotp)
{
if (finalDigit == null)
return NoContent();
else if (finalDigit == sentotp && ***30 Second Check Here***)
{
return Ok("Activated Successfully");
}
else if(!(***30 Second Check Here***))
{
return BadRequest("OTP Timedout");
}
else
{
return BadRequest("Please Enter Valid OTP");
}
}
}
}
**View**
@{
ViewData["Title"] = "GenerateOtp";
}
<h1>GenerateOtp</h1>
<form method="post" asp-action="SendOtp" asp-controller="Home">
<br />
<input type="submit" value="GetOtp" class="btn btn-primary btn-lg"/>
<br />
<div>
@TempData["otp"]
</div>
<br />
<input type="number"/>
<br />
<input type="submit" value="SubmitOtp" class="btn btn-primary btn-lg"/>
</form>
与保存到临时数据中的OTP相同,您可以在SendOtp Action方法中保存发送OTP时的时间戳。在 SubmitOtp 操作方法中,从 TempData 中读取该时间戳。如果当前时间戳和 TempData 时间戳之间的差异超过 30 秒,则拒绝请求。
public IActionResult GenerateOtp()
{
return View();
}
[HttpPost]
public IActionResult SendOtp()
{
string num = "01223456789";
int len = num.Length;
string otp = string.Empty;
int otpDigit = 4;
string finalDigit;
int getIndex;
for (int i = 0; i < otpDigit; i++) {
do
{
getIndex = new Random().Next(0, len);
finalDigit = num.ToCharArray()[getIndex].ToString();
} while (otp.IndexOf(finalDigit) != -1 );
otp += finalDigit;
}
TempData["otp"] = otp;
TempData["timestamp"] = DateTime.Now;
return RedirectToAction("GenerateOtp", "Home");
}
[HttpPost]
public IActionResult SubmitOtp([FromForm] int finalDigit, int sentotp)
{
if (finalDigit == null)
return NoContent();
else if (finalDigit == sentotp && **30 Second Check Here**)
{
return Ok("Activated Successfully");
}
else if((DateTime.Now - Convert.DateTime(TempData["timestamp"])).TotalSeconds > 30)
{
return BadRequest("OTP Timedout");
}
else
{
return BadRequest("Please Enter Valid OTP");
}
}