gpt4 book ai didi

asp.net - 如何防止用户重新提交表单?

转载 作者:行者123 更新时间:2023-12-01 07:43:22 25 4
gpt4 key购买 nike

我正在开发一个单页应用程序

在应用程序结束时,用户可以提交他的联系信息(姓名、电话号码等)。这将发送一封电子邮件并将页面修改为“感谢提交 [...]”页面。

问题是,客户可以按返回按钮并重新发送电子邮件。

有没有办法防止这种……垃圾邮件?

代码

Sub BT_Send(sender As Object, e As EventArgs) Handles BT_Send.Click
Try
'Creating the Email Message
Dim mailMessage As New MailMessage()
mailMessage.To.Add("SomeOne@a.com")
mailMessage.From = New MailAddress("Robot@a.com", "Robot")
mailMessage.Subject = "Test"
mailMessage.IsBodyHtml = True
mailMessage.Body = LBL_Emailbody.Text & _
"<br><br><br><div style=""font-size: 0.7em;"">Robot speaking, I will not answer if you send me a message.</div>"
Dim smtpClient As New SmtpClient("Something.com")
smtpClient.Send(mailMessage)


PNL_Before.Visible = False
PNL_After.Visible = True
Catch ex As Exception
LBL_errorEmail.Visible = True
'Should never happen...
End Try
End sub

最佳答案

这是一个非常简单的示例,我在页面上使用静态变量并避免使用数据库。

asp.net 页面是

<asp:Literal runat="server" ID="txtInfos"></asp:Literal><br />
<asp:TextBox runat="server" ID="txtEmail"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />/>

以及背后的代码。

static Dictionary<string, DateTime> cLastSubmits = new Dictionary<string, DateTime>();

private static readonly object syncLock = new object();

protected void Button1_Click(object sender, EventArgs e)
{
DateTime cWhenLast;

lock (syncLock)
{
var cNowIs = DateTime.UtcNow;
if (cLastSubmits.TryGetValue(txtEmail.Text, out cWhenLast))
{
if (cWhenLast > cNowIs )
{
txtInfos.Text = "Please contact us again after 10 seconds";
return;
}
else
{
// ok I let him submit the form, but note the last date time.
cLastSubmits.Remove(txtEmail.Text);
}
}

foreach(var DelMe in cLastSubmits.Where(x => cNowIs > x.Value).ToList())
cLastSubmits.Remove(DelMe.Key);

// if reach here, note the last datetime of submit
cLastSubmits.Add(txtEmail.Text, cNowIs.AddSeconds(10));
}

// and submit the form.
txtInfos.Text = "thank you for submit the form";
}

一些笔记。

关于asp.net - 如何防止用户重新提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25040281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com