提问人:M_Griffiths 提问时间:11/9/2023 更新时间:11/9/2023 访问量:161
Google reCaptcha v2 无法始终如一地工作
Google reCaptcha v2 not working consistently
问:
我以前从未实施过 Google Captcha,所以我不知道问题出在哪里。当我提交表格时,有时它有效,有时无效。如果我在未选中验证码框的情况下提交表单,然后在将我发送回页面时单击验证码框,则它不会发送电子邮件。关于如何解决此问题的任何想法?
我的代码背后:
protected static string ReCaptcha_Key = "<key>";
protected static string ReCaptcha_Secret = "<key>";
public bool IsReCaptchValid()
{
var result = false;
var captchaResponse = Request.Form["g-recaptcha-response"];
var secretKey = ReCaptcha_Secret;
var apiUrl = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}";
var requestUri = string.Format(apiUrl, secretKey, captchaResponse);
var request = (HttpWebRequest)WebRequest.Create(requestUri);
using (WebResponse response = request.GetResponse())
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
JObject jResponse = JObject.Parse(stream.ReadToEnd());
var isSuccess = jResponse.Value<bool>("success");
result = (isSuccess) ? true : false;
}
}
return result;
}
protected void btnSend_Click(object sender, EventArgs e)
{
string Name = txtName.Text;
string EmailAddress = txtEmail.Text;
string phone = "Number not supplied";
if(txtPhone.Text != "")
{
phone = txtPhone.Text;
}
string SendAddress = @"<email address>";
string Subject = "Message from " + Name + " Top Tree Fellas Website";
string Message = Regex.Replace(txtMsg.Text, @"\r\n?|\n", "<br />");
if (String.IsNullOrEmpty(Name) || String.IsNullOrEmpty(EmailAddress) || String.IsNullOrEmpty(Message) || !(IsReCaptchValid()))
{
lblError.Text = "Please ensure all required fields are filled in.";
}
else
{
try
{
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(@"<email address>"));
msg.Subject = Subject;
msg.From = new MailAddress(SendAddress);
msg.IsBodyHtml = true;
msg.Body = "<p><strong>Contact Name:</strong> " + Name + "<br />";
msg.Body += "<strong>Email Address:</strong> " + EmailAddress + "<br />";
msg.Body += "<strong>Phone:</strong> " + phone + "<br />";
msg.Body += "<strong>Message:</strong><br />" + Message + "</p>";
msg.ReplyToList.Add(new MailAddress(EmailAddress));
SmtpClient smtpClnt = new SmtpClient("smtp.ionos.co.uk");
smtpClnt.UseDefaultCredentials = false;
smtpClnt.Port = 587;
smtpClnt.Credentials = new NetworkCredential(@"<email address>", "<password>");
smtpClnt.EnableSsl = true;
smtpClnt.Send(msg);
lblError.ForeColor = System.Drawing.Color.Green;
txtName.Text = "";
txtEmail.Text = "";
txtMsg.Text = "";
txtPhone.Text = "";
Response.Write("<script>alert('Message sent');</script>");
}
catch (Exception er)
{
string er1 = er.Message + "<br />" + er.InnerException;
lblError.Text = er1;
Response.Write("<script>alert('Something went wrong. Please try again later or call us on: 0<phone number>');</script>");
}
}
}
我的标记:
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-type="image" data-sitekey="6LfDHwcpAAAAAL71bUOZ4291MqxqDyTfvGbzOMcg"></div>
<asp:Button ID="btnSend" runat="server" ValidationGroup="vldContact" Text="Send your message" CssClass="btn btn-danger" OnClick="btnSend_Click" />
我的webconfig:
<system.net>
<defaultProxy>
<proxy usesystemdefault = "false" bypassonlocal="false" proxyaddress="http://ntproxyus.lxa.perfora.net:3128"/>
</defaultProxy>
</system.net>
我得到的例外:
Failure sending mail.
System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Mail.SmtpPooledStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at System.IO.Stream.Dispose() at System.Net.ConnectionPool.Destroy(PooledStream pooledStream) at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)
答:
0赞
Steve Py
11/9/2023
#1
SmtpClient 看起来没有得到清理,当对客户端的先前引用最终被垃圾回收时,多个句柄/连接可能会在辅助调用中被引用。
尝试确保 SmtpClient 被释放:
using (SmtpClient smtpClnt = new SmtpClient("smtp.ionos.co.uk"))
{
smtpClnt.UseDefaultCredentials = false;
smtpClnt.Port = 587;
smtpClnt.Credentials = new NetworkCredential(@"<email address>", "<password>");
smtpClnt.EnableSsl = true;
smtpClnt.Send(msg);
}
评论