[英]Recaptcha not working in asp.net
我正在使用Recaptcha停止在我的网站上发送垃圾邮件。 这是Recaptcha的代码
<asp:Panel ID="Panel1" runat="server" style="padding:5px" BackColor="White"
BorderColor="#999999" BorderStyle="Solid">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<br />
<recaptcha:RecaptchaControl ID="recaptcha" runat="server"
PrivateKey="XXXX-HIDDEN-XXXX"
PublicKey="XXXX-HIDDEN-XXXX" Theme="Red" />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
<asp:Button ID="Button3" runat="server" Text="Cancel" />
<asp:Label ID="lblResult" runat="server" Font-Size="Medium" ForeColor="Red"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:ModalPopupExtender ID="UpdatePanel1_ModalPopupExtender" runat="server"
Enabled="True" PopupControlID="Panel1" TargetControlID="Button2" BackgroundCssClass="modalBackground" CancelControlID="Button3">
</asp:ModalPopupExtender>
Button1_Click
的代码:
protected void Button1_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
lblResult.Text = "All Good";
}
else
{
lblResult.Text = "The words you entered are incorrect";
}
}
该代码的目的是,每当用户在Recaptcha中输入内容时,如果单词正确,则将显示“ All Good”,如果不是,则显示“您输入的单词不正确”。
但是问题是当我单击Button1
,不会执行其Click
事件。 但是,当未加载if(Page.isValid)
时(即,未连接Internet时), Button1_Click()
执行并仅执行if(Page.isValid)
条件之外的代码。 例如,如果我这样修改click事件:
protected void Button1_Click(object sender, EventArgs e)
{
lblResult.Text="Not in if condition";
Page.Validate();
if (Page.IsValid)
{
lblResult.Text = "All Good";
}
else
{
lblResult.Text = "The words you entered are incorrect";
}
}
然后,它成功地将lblResult.Text
设置为"Not in if condition"
。 但是,加载Recaptcha时,即使If
条件之外的那些行也不会执行。
我试图解决的问题:
请告诉我有关此问题的其他任何解决方案及其发生原因。
我也有同样的问题,不要使用控件,直接使用api; 即:
标记:
<div>
<asp:Literal ID="litResult" runat="server" Mode="Encode" />
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=<%= RecaptchaPublicKey %>">
</script>
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" runat="server" />
</div>
后面的代码:
private const string RECAPTCHA_CHALLENGE_FIELD = "recaptcha_challenge_field";
private const string RECAPTCHA_RESPONSE_FIELD = "recaptcha_response_field";
protected string RecaptchaPublicKey {
get { return ConfigurationManager.AppSettings["RecaptchaPublicKey"]; }
}
protected void btnSubmit_Click(object sender, EventArgs e) {
var validator = new Recaptcha.RecaptchaValidator {
PrivateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"],
RemoteIP = Request.UserHostAddress,
Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD],
Response = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD]
};
if (validator.Validate().IsValid) {
litResult.Text = "All Good";
} else {
litResult.Text = "The words you entered are incorrect";
}
}
也不为我工作-也许是因为我设置了ValidationGroup
,也许不是。 我通过强制它进行验证,然后在回发后检查它是否有效来修复它:
recaptcha.Validate();
if (recaptcha.IsValid) { /* ...do stuff... */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.