Recaptcha不能在asp.net应用程序中工作

时间:2011-08-14 13:34:22

标签: c# asp.net recaptcha

我正在尝试在我的应用程序中使用recaptcha控件。出于某种原因,它始终有效。无论我在recaptcha字段中放置什么值,它始终有效。知道我错过了什么吗?

这是我的代码

<%@ Control Language="C#" CodeBehind="RegisterControl.ascx.cs" AutoEventWireup="true"
    Inherits="UserControls.RegisterControl" %>
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
<asp:Panel ID="PlReg" runat="server" DefaultButton="CreateUserButton">
    <div class="Reg_Wrapper">
        <span class="failureNotification">

            <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
        </span>
        <div class="Clear">
        </div>
        <div class="Reg_ControlName">
            <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label></div>
        <div class="Reg_Control">
            <asp:TextBox ID="TxtUserName" runat="server" CssClass="textEntry"></asp:TextBox>
            <asp:RequiredFieldValidator Display="Dynamic" ID="UserNameRequired" runat="server"
                ControlToValidate="TxtUserName" CssClass="failureNotification" ErrorMessage="User Name is required."
                ToolTip="User Name is required." ValidationGroup="RegisterUserValidationGroup"><img src="/Images/required.png" alt="Required" /></asp:RequiredFieldValidator>
        </div>
        <div class="Clear">
        </div>
        <div class="Reg_ControlName">
            <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></div>
        <div class="Reg_Control">
            <asp:TextBox ID="TxtEmail" runat="server" CssClass="textEntry"></asp:TextBox>
            <asp:RequiredFieldValidator Display="Dynamic" ID="EmailRequired" runat="server" ControlToValidate="TxtEmail"
                CssClass="failureNotification" ErrorMessage="E-mail is required." ToolTip="E-mail is required."
                ValidationGroup="RegisterUserValidationGroup"><img src="/Images/required.png" alt="Required" /></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RevEmail" ControlToValidate="TxtEmail" runat="server"
                ErrorMessage="*" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
            <img src="/Images/required.png" alt="Required" />
            </asp:RegularExpressionValidator>
        </div>
        <div class="Clear">
        </div>
        <div class="Reg_ControlName">
            <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></div>
        <div class="Reg_Control">
            <asp:TextBox ID="TxtPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
            <asp:RequiredFieldValidator Display="Dynamic" ID="PasswordRequired" runat="server"
                ControlToValidate="TxtPassword" CssClass="failureNotification" ErrorMessage="Password is required."
                ToolTip="Password is required." ValidationGroup="RegisterUserValidationGroup"><img src="/Images/required.png" alt="Required" /></asp:RequiredFieldValidator>
        </div>
        <div class="Clear">
        </div>
        <div class="Reg_ControlName">
            <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Conf. Password:</asp:Label></div>
        <div class="Reg_Control">
            <asp:TextBox ID="TxtConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
            <asp:RequiredFieldValidator ControlToValidate="TxtConfirmPassword" CssClass="failureNotification"
                Display="Dynamic" ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired"
                runat="server" ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup"><img src="/Images/required.png" alt="Required" /></asp:RequiredFieldValidator>
            <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="TxtPassword"
                ControlToValidate="TxtConfirmPassword" CssClass="failureNotification" Display="Dynamic"
                ErrorMessage="The Password and Confirmation Password must match." ValidationGroup="RegisterUserValidationGroup"><img src="/Images/required.png" alt="Required" /></asp:CompareValidator>
        </div>
        <div class="Clear">
        </div>
        <div class="Reg_ControlName">
            &nbsp;</div>
        <div class="Reg_Control">
            <recaptcha:RecaptchaControl ID="recaptcha" runat="server" PublicKey="my public key"
                PrivateKey="my private key" Theme="white" />
        </div>
        <div class="Clear">
        </div>
        <div class="Reg_ControlName">
            &nbsp;</div>
        <div class="Reg_Control">
            <asp:Button ID="CreateUserButton" role="button" runat="server" CommandName="MoveNext"
                Text="Create User" ValidationGroup="RegisterUserValidationGroup" CssClass="Reg_Button"
                OnClick="CreateUserButton_Click" /></div>
        <div class="Clear">
        </div>
    </div>
</asp:Panel>

服务器侧

protected void CreateUserButton_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        ErrorMessage.Text = "page is valid";
    }
    else
    {
        ErrorMessage.Text = "page is not valid";
    }
}

2 个答案:

答案 0 :(得分:0)

这是适合我的一个。务必在服务器上插入正确的密钥:

<recaptcha:RecaptchaControl ID="recaptcha" Theme="clean" runat="server"
    PublicKey="hiddenxxx" TabIndex="7" ToolTip="Accessible CAPTCHA" 
        privateKey="hiddenxxx" />
<asp:Label CssClass="captchaLabel" ID="lblResult" runat="server" />

如您所见,我通常将CAPTCHA错误消息与其他消息分开。

代码隐藏:

if(Page.IsValid)
{
    ...
}
else
{
    lblResult.Text = "Incorrect!";
    lblResult.ForeColor = System.Drawing.Color.Red;
}

答案 1 :(得分:0)

我不认为在页面加载时检查验证码验证应该有你提交的按钮,你在按钮的点击事件中检查出来,这就是我的使用方法。我想你应该查看链接blove ...

http://code.google.com/intl/tr-TR/apis/recaptcha/docs/aspnet.html

http://www.codeproject.com/KB/custom-controls/CaptchaControl.aspx