Here I want OTP Session Time out Required. Wen I click the GenerateOtp Click Functionality In That Time Otp Is Generated . That OTP I want show The TimeOut like 30 sec.
Loading
Here I want OTP Session Time out Required. Wen I click the GenerateOtp Click Functionality In That Time Otp Is Generated . That OTP I want show The TimeOut like 30 sec.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 18, 2024, 4:47 AM EST
To implement an OTP session timeout in a .NET application, you can utilize a combination of a timer and a simple user interface update.
public class OtpService { private static string _otp; private static DateTime _otpGeneratedTime; public string GenerateOtp() { _otp = new Random().Next(100000, 999999).ToString(); // Generate a 6-digit OTP _otpGeneratedTime = DateTime.Now; return _otp; } public bool IsOtpValid() { return (DateTime.Now - _otpGeneratedTime).TotalSeconds < 30; // Check if OTP is still valid } }In your front-end, you can use JavaScript to handle the countdown timer:
let countdown = 30; const otpDisplay = document.getElementById('otpDisplay'); function startTimer() { const timer = setInterval(() => { if (countdown <= 0) { clearInterval(timer); otpDisplay.innerText = "OTP expired"; } else { otpDisplay.innerText = `OTP valid for ${countdown--} seconds`; } }, 1000); } // Call startTimer() after generating the OTPThis implementation generates an OTP and starts a countdown timer, displaying the remaining time until expiration. After 30 seconds, the OTP is considered invalid.
Sangeetha SRank #212 · 11.4K reputationPosted Nov 18, 2024, 5:42 AM EST
OTP session timeout Example
Front end
OTP Generation #otp-container { display: none; } Generate OTPYour OTP: Time remaining: 30 seconds let timeout; document.getElementById('generateOtpBtn').addEventListener('click', function() { const otp = Math.floor(100000 + Math.random() * 900000); // Generates a 6-digit OTP document.getElementById('otp').innerText = otp; document.getElementById('otp-container').style.display = 'block'; let timeRemaining = 30; document.getElementById('timer').innerText = timeRemaining; timeout = setInterval(function() { timeRemaining -= 1; document.getElementById('timer').innerText = timeRemaining; if (timeRemaining.NET (Backend):
[ApiController] [Route("api/[controller]")] public class OtpController : ControllerBase { [HttpGet("generate")] public IActionResult GenerateOtp() { Random random = new Random(); int otp = random.Next(100000, 999999); // Generates a 6-digit OTP return Ok(new { Otp = otp }); } }Jagapathibabu Banoth166 reputationPosted Nov 18, 2024, 6:42 AM EST
but here not decrese the time why
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 18, 2024, 6:50 AM EST
Hi
Have you called the below function in your code?
// Call startTimer() after generating the OTP startTimer()If not, call the function above and check again.
If the issue still exists, share the code you tried, and I’ll assist you.
Harshit PandeyRank #582 · 4.1K reputationPosted Nov 20, 2024, 5:21 AM EST
To implement an OTP (One-Time Password) generation functionality with a 30-second timeout, you can follow these steps:
Approach