OTP到期检查ASP.NET核心MVC

发布于 2025-01-20 15:52:20 字数 2174 浏览 0 评论 0原文

我正在构建一个 ASP.NET Core 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>

I'm building an ASP.NET Core MVC Application in which I send OTP to the user and the user has to enter the OTP within 30 seconds for the OTP to work. How can I check that the OTP entered by the user in the input field is entered within 30 seconds of the OTP generated?

I have already written the Controller for Generating, Getting & Submitting the OTP. Just need to know the logic of time checking of OTP Generation.

Controller

 [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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2025-01-27 15:52:20

与保存到临时数据中的 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");
            }
        }

Same as OTP that you saved into the temp data, you can save the timestamp when OTP is sent in the SendOtp Action method. While in the SubmitOtp action method, read that timestamp from the TempData. If the difference between the current and TempData timestamp is more than 30 seconds then reject the request.

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");
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文