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

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

我试图在我的应用程序中用户验证码控制。出于某种原因,它总是有效的。无论我在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";
    }
}

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

这里有一个适合我的。请确保在服务器上插入正确的密钥:

<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;
}

我不认为检查验证码在页面加载应该有提交按钮,你有,你检查它在点击事件的按钮,这就是我如何使用。我认为你应该检查链接,爱…

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

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