65.9K
CodeProject 正在变化。 阅读更多。
Home

Recaptcha

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6投票s)

2011年1月4日

CPOL

2分钟阅读

viewsIcon

30890

Recaptcha:验证码可以防止垃圾邮件发送者攻击我们的网站。有很多验证码控件可用于 asp.net,但其中一些可能在回发时无法正常工作。

Recaptcha

验证码可以防止垃圾邮件发送者攻击我们的网站。有很多验证码控件可用于 asp.net,但其中一些可能在回发时无法正常工作。Google 为 asp.net 开发者发布了一个名为“recaptcha”的验证码控件,它易于集成,更安全、可定制,并且完全免费,你只需要一个 Gmail 账号即可。


要在你的网站上使用“recaptcha”控件,你必须注册你的网站并获取 API 密钥。


访问链接 https://www.google.com/recaptcha/admin/create


注册你的网站后,Google 将提供两个密钥:
•    公钥和
•    私钥 


要将 recaptcha 与 ASP.NET 一起使用,你必须下载 DLL 文件


http://code.google.com/p/recaptcha/downloads/detail?name=recaptcha-dotnet-1.0.5.0-binary.zip&can=2&q=label%3Aaspnetlib-Latest


现在下载的文件夹包含一个文件 Recaptcha.dll,将该文件添加到你的项目的 bin 文件夹中,并引用该 DLL 到你的项目。
现在创建一个空白 aspx 页面并添加页眉到页面

<% @ Register TagPrefix ="recaptcha" Namespace ="Recaptcha" Assembly ="Recaptcha" %>

<recaptcha:RecaptchaControl ID="recap" PublicKey="Yourpublic Key" PrivateKey="Your Private Key" runat="server" />

要验证此 recaptcha,在按钮点击事件中添加以下几行代码:

            if (Page.IsValid == true )
            {
                Label1.Text = "Capacta Right";
            }
            else
            {
                Label1.Text = "Wrong";
            }

自定义
           你可以为 recaptcha 控件设置主题。默认情况下,设置为红色主题。
有四种主题:


<recaptcha:RecaptchaControl ID="recap" PublicKey="Yourpublic Key" PrivateKey="Your Private Key" runat="server" Theme="clean" /> 

 简洁主题比其他主题看起来更专业。


自定义主题和大小
             我们可以创建自己的自定义主题并调整 recaptcha 的大小。为此,首先将主题属性设置为自定义。

<recaptcha:RecaptchaControl ID="recap" PublicKey="Yourpublic Key" PrivateKey="Your Private Key" runat="server" Theme="custom" CustomThemeWidget="recaptcha_widget"   /> 

验证码包含图像、文本框、刷新按钮、音频验证码和帮助按钮,我们可以根据需要删除任何这些按钮,为此将脚本粘贴到设计源中的任何位置。

   <div id="recaptcha_widget" style=" display : none">
   < div id ="recaptcha_image"></div>
   <div class="recaptcha_only_if_incorrect_sol" style=" color : red"> Incorrectplease try again </div>
   <span class="recaptcha_only_if_image"> Enter thewords above: </span>
   <span class="recaptcha_only_if_audio"> Enter thenumbers you hear: </span>
   <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
   <div><a href="javascript:Recaptcha.reload()"> Getanother CAPTCHA </a></div>
   <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')"> Getan audio CAPTCHA </a></div >

   <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')"> Getan image CAPTCHA </ a ></ div >
   <div><a href="javascript:Recaptcha.showhelp()"> Help </a></div>
  </div>
  <script type ="text/javascript"
    src="http://www.google.com/recaptcha/api/challenge?k=your_public_key">
  </script >
  <noscript>
   <iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
        height="300" width="500" frameborder="0"></iframe><br>
   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
   </textarea>
   <input type="hidden" name ="recaptcha_response_field"
        value="manual_challenge">
  </noscript>

 粘贴上述代码时需要注意两点:


1. Div 标签必须具有与 CustomThemeWidget="recaptcha_widget" 相同的 ID
2. 在上述代码片段中替换你的公钥。(在 Script 标签内)

现在自定义 验证码 的外观如下:

 

 

 

我们可以删除“获取另一个验证码”或“获取音频验证码”或“帮助”。

现在我们尝试删除“获取音频验证码”和“帮助”链接。
为此,只需从上述代码片段中删除以下两行:
 

<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')"> Getan audio CAPTCHA </a></div >
<div><a href="javascript:Recaptcha.showhelp()"> Help </a></div>

 要更改背景颜色,请在 ID 为“recaptcha_widget”的 css 类中定义背景颜色

<div id="recaptcha_widget" style="background-color:#f7f7f7;"> 
 要更改宽度
<div id="recaptcha_widget" style="background-color:#f7f7f7;width:300px;">

宽度应至少为 300px。

使用 recaptcha 的网站有 facebook、wikipedia、tinypic 等…

 

 

 

 

 

© . All rights reserved.