【问题标题】:Setting timeout for 1 minute in asp.net在asp.net中设置超时1分钟
【发布时间】:2019-01-06 06:00:26
【问题描述】:

我想将 OTP 设置为 1 分钟,以便在 1 分钟后过期。我正在通过电子邮件发送 OTP,它正在工作,但 1 分钟后 OTP 没有过期。

下面是我的代码。

这是.cs文件

 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";
        }


    }
}
}

这是 .aspx 文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OTPThroughEmail.aspx.cs" Inherits="OTP_Generation.OTPThroughEmail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <label id="l" >Enter OTP digit</label>
    <asp:TextBox ID="textBox" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Sent_OTP" Text="Sent OTP" runat="server" 
        onclick="Sent_OTP_Click" />
    <asp:Button ID="Verify_OTP" Text="Verify OTP" runat="server" 
        onclick="Verify_OTP_Click" />
    <br /><br />
    <asp:Label ID="msg" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="exception" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

我还在 Web.config 文件的“system.web”中将“sessionState”设置为

<sessionState cookieless="false" timeout="1"></sessionState>

当我运行上述代码时,每次都会验证 OTP。 我也尝试使用 Session(用 Session 替换 ViewState),但它在以下行中抛出 NullReferenceException:

if(textBox.Text.Equals(Session["otp"].ToString()))

我在网上搜索了1分钟后找不到设置过期的方法。我也试过this,但没用。

请帮忙... 谢谢

【问题讨论】:

  • 将当前时间保存在Sent_OTP_Click 处理程序中,并使用ViewState 或(更好)Session(以防访问者重新加载页面)在Verify_OTP_Click 中检索它。 sessionState 与您无关。
  • 简单来说,你能不能多加一个视图状态,叫做otp expiryTime,当你验证你的otp时,你会先检查expiryTime视图状态。如果过期(检查 DateTime.Now > expiryTime),如果过期,则只需返回错误消息并清理 ViewState
  • 您应该将 otp 及其到期时间存储在一些持久性存储中。 ViewState 和 session 不是此类功能的可靠方式。
  • 感谢@SKLTEZ。它奏效了。
  • @ChetanRanpariya 如何使用实体框架进行 Web 表单应用程序?

标签: c# asp.net session viewstate one-time-password


【解决方案1】:

使用 Random 生成 OTP 是一种不好的做法。查看 TOTP 算法 (RFC 6238),该算法专为生成有效性有限的 OTP 而设计。

另外,请注意,通过电子邮件发送 OTP 并不是真正的双因素身份验证。

也许,对于您的情况,电子邮件中的 OTP 和 Random 作为生成器都可以,但通常应避免这种做法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2017-04-26
    • 1970-01-01
    • 2022-01-25
    • 2017-09-26
    • 2011-10-18
    • 2013-06-05
    相关资源
    最近更新 更多