title: Setting timeout for 1 minute in asp.net
body: <p>I want to set OTP for 1 minute so that it can be expired after 1 minute. I am sending OTP through the email and it is working but the OTP is not getting expired after 1 minute. </p>
  
  <p>below is my code.</p>
  
  <p>This is .cs file</p>
  
  <pre><code> using System;
   using System.Collections;
   using System.Configuration;
   using System.Data;
   using System.Linq;
   using System.Web;
   using System.Web.Security;
   using System.Web.UI;
   using System.Web.UI.HtmlControls;
   using System.Web.UI.WebControls;
   using System.Web.UI.WebControls.WebParts;
   using System.Xml.Linq;
   using System.Net.Mail;
   using System.Net;
  
   namespace OTP_Generation
   {
   public partial class OTPThroughEmail : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void Sent_OTP_Click(object sender, EventArgs e)
      {
  
          //generate otp
          Random rand = new Random();
          string digits = rand.Next(0, 999999).ToString("D6");
  
          ViewState["otp"] = digits;
  
          bool check = sendMail("Your OTP is :" + digits);
  
          if (check)
          {
              msg.Text = "OTP sent successfully";
          }
          else
          {
              msg.Text = "Error in sending Email";
          }
  
      }
  
      public bool sendMail(string msg)
      {
          bool check = false;
          try
          {
              MailMessage mail = new MailMessage("myid@gmail.com", "myid@gmail.com");
              mail.Subject = "Verify code";
              mail.Body = msg;
  
              SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
              smtp.UseDefaultCredentials = false;
  
              smtp.Credentials = new NetworkCredential("my@gmail.com", "******");
  
              smtp.EnableSsl = true;
              smtp.Send(mail);
              check = true;
          }
          catch (Exception e)
          {
              exception.Text = e.Message;
              check = false;
          }
          return check;
  
      }
  
      protected void Verify_OTP_Click(object sender, EventArgs e)
      {
          if (textBox.Text.Equals(""))
          {
              msg.Text = "Please Enter the OTP";
              return;
          }
  
          if(textBox.Text.Equals(ViewState["otp"].ToString()))
          {
              msg.Text = "OTP verified";
          }
          else
          {
              msg.Text = "OTP is wrong";
          }
  
  
      }
  }
  }
  </code></pre>
  
  <p>This is .aspx file</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OTPThroughEmail.aspx.cs" Inherits="OTP_Generation.OTPThroughEmail" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      &lt;label id="l" &gt;Enter OTP digit&lt;/label&gt;
      &lt;asp:TextBox ID="textBox" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;br /&gt;
      &lt;asp:Button ID="Sent_OTP" Text="Sent OTP" runat="server" 
          onclick="Sent_OTP_Click" /&gt;
      &lt;asp:Button ID="Verify_OTP" Text="Verify OTP" runat="server" 
          onclick="Verify_OTP_Click" /&gt;
      &lt;br /&gt;&lt;br /&gt;
      &lt;asp:Label ID="msg" runat="server" &gt;&lt;/asp:Label&gt;
      &lt;br /&gt;
      &lt;asp:Label ID="exception" runat="server"&gt;&lt;/asp:Label&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I also set "sessionState" inside "system.web" in Web.config file as</p>
  
  <pre><code>&lt;sessionState cookieless="false" timeout="1"&gt;&lt;/sessionState&gt;
  </code></pre>
  
  <p>When I run above code OTP is verified everytime.
  I also tried using Session(replacing ViewState with Session) but it is throwing NullReferenceException in following line:</p>
  
  <pre><code>if(textBox.Text.Equals(Session["otp"].ToString()))
  </code></pre>
  
  <p>I searched online but couldn't find the way to set expiration after 1 minute. I also tried <a href="https://stackoverflow.com/questions/18503784/is-it-possible-to-set-session-timeout-to-1-minute-in-asp-net">this</a> but it didn't work.</p>
  
  <p>Please help...
  Thanks</p>