2010-03-31 28 views
1

我试图在.NET 1.1项目,该项目可以在这里找到使用旧版本的ReCaptcha验证的:http://recaptcha.googlecode.com/svn/trunk/recaptcha-plugins/dotnet-old/src/Recaptcha/的Recaptcha验证在.NET 1.1项目

我的代码是非常相似的例子

<asp:TextBox ID="EmailAddress" runat="server"></asp:TextBox> 
<recatpcha:RecaptchaValidator ID="RecaptchaValidator1" runat="server" Theme="Clean" PublicKey="xxxxxxxxxxxxxx" PrivateKey="xxxxxxxxxx" ControlToValidate="EmailAddress"></recatpcha:RecaptchaValidator> 
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="true" OnClick="Button1_Click" /> 

我想的行为很简单:可在上面的链接中找到的网页加载时,用户会看到一个字段中输入他们的电子邮件地址,验证码即可完成,并提交按钮。提交时,如果我们的数据库中找不到用户使用该电子邮件地址,或者验证码的回答不正确,则会显示一个新的验证码以及相应的错误消息。否则,他们将收到一封关于如何重置密码的电子邮件。

的主要问题是,验证码不能在页面加载显示。然而,当我点击提交按钮时,触发Page.Validate(),页面被重新加载并显示验证码。

在此基础上,我想我会尝试不同的方法,并在页面加载事件调用Page.Validate()来获取验证码最初露面。这几乎奏效了:验证码出现在第一页加载时,但是当一个无效的电子邮件与正确的验证码答案一起提交时,验证码在页面重新加载时消失,但当然不能发送电子邮件。

如何我要么强制验证码当页面最初打被渲染,或者,防止输入无效的电子邮件地址时,所提交的验证码?

回答

1

嗯,它不漂亮,但我得到它的工作。

由于使用RecaptchaValidator标签没有工作,我想它的方式,我用的ReCaptcha AJAX API(http://recaptcha.net/apidocs/captcha/client.html)嵌入在网页上正是如此小部件:

$(document).ready(function() { 
    Recaptcha.create("<public key>", 
     "ReCaptcha", { 
     theme: "clean", 
     callback: Recaptcha.focus_response_field 
    }); 
}); 

而且,由于所有的数据需要验证与recaptcha服务器(挑战,公钥等,更多信息在这里:http://recaptcha.net/apidocs/captcha/)是在POST中,我实例化一个新的RecaptchaValidator,设置其余属性,并调用方法来验证用户响应。

protected void Submit_Click(object sender, System.EventArgs e) 
{ 
    RecaptchaValidator RecaptchaValidator1 = new RecaptchaValidator(); 
    RecaptchaValidator1.Page = this; 
    RecaptchaValidator1.PrivateKey = "<private key>"; 
    RecaptchaValidator1.PublicKey = "<public key>"; 

    bool CaptchaPassed = RecaptchaValidator1.ValidateUserResponse(); 

    if (CaptchaPassed) 
    {  
     //hide the captcha, do some stuff 
    }  
} 

不可否认,有点不好意思,但它完成了工作。

注意 - 我在问题中提到了这一点,只是重申:此服务器端代码使用老,.NET 1.1兼容的reCAPTCHA插件。问题在于插件的代码链接。我有令人尴尬的低代表,并且不能再使用超链接>。 <