验证码在asp.net中不起作用

本文关键字:不起作用 net asp 验证 | 更新日期: 2023-09-27 18:17:05

我正在使用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" />
                    &nbsp;
                    <asp:Button ID="Button3" runat="server" Text="Cancel" />
                    &nbsp;<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事件没有执行。但是,当验证码没有加载时(即当互联网没有连接时),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条件之外的行也不会执行。

我想解决的问题:

  1. 删除更新面板:相同的问题
  2. 在实际域名上测试,同样的问题
  3. 调试和检查任何异常:没有异常显示在输出>调试屏幕

请告诉我这个问题的其他解决方案和发生的原因。

验证码在asp.net中不起作用

我也有同样的问题,不要使用控件,直接使用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设置,也许不是。我通过强制它验证然后检查它是否在post-back上有效来修复它:

recaptcha.Validate();
if (recaptcha.IsValid) { /* ...do stuff... */ }