2016年5月26日 星期四

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用 (使用 GoogleReCaptcha.dll )

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用 (使用 GoogleReCaptcha.dll )

2016-05-26

Google reCAPTCHA
https://www.google.com/recaptcha/intro/index.html

你的網址 (例如 : forum.shaurong.idv.tw ) 註冊一個 reCAPTCHA 使用
https://www.google.com/recaptcha/admin#list

為了在本機測試,我用 localhost 去申請,沒想到過了 (其實並不合適)

參考這篇

Google ReCaptcha 2.0 - ASP.net Control
http://www.codeproject.com/Tips/884193/Google-ReCaptcha-ASP-net-Control

下載 GoogleReCaptcha_Binary.zip,解壓,把 GoogleReCaptcha.dll 放到你的方案的 bin 目錄 ( 以小弟而言是 D:\CodeTemp\WebApplication4\WebApplication4\bin 目錄 )

在 Default.aspx 增加 黃色部分,注意某些部分要加在 <form> 和 </form> 之間,橘色的 KEY 要換成自己剛剛根據 Internet 網域申請的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4.Default" %>
<%@ Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <cc1:GoogleReCaptcha ID="ctrlGoogleReCaptcha" runat="server" PublicKey="YOUR_SITE_KEY" PrivateKey="YOUR_SECRET_KEY" />
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
        <asp:Label ID="lblStatus" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />
    </div>
    </form>
 
</body>
</html>


Default.aspx.cs 增加 黃色部分

using System;

namespace WebApplication4
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        GoogleReCaptcha.GoogleReCaptcha ctrlGoogleReCaptcha = new GoogleReCaptcha.GoogleReCaptcha();
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            ctrlGoogleReCaptcha.PublicKey = "YOUR_SITE_KEY";
            ctrlGoogleReCaptcha.PublicKey = "6Lf6cCATAAAAAKs1vRwMjOpfHzY2qVsni6O55BI1";
            ctrlGoogleReCaptcha.PrivateKey = "YOUR_SECRET_KEY";
            ctrlGoogleReCaptcha.PrivateKey = "6Lf6cCATAAAAAKzY_i_Ol-dwpDgp21fllXOQUAzZ";
            this.Panel1.Controls.Add(ctrlGoogleReCaptcha);
            //this.form1.Controls.Add(ctrlGoogleReCaptcha);
        }
        protected void btn_Click(object sender, EventArgs e)
        {
            if (ctrlGoogleReCaptcha.Validate())
            {
                //submit form success
                lblStatus.Text = "Success";
            }
            else
            {
                //captcha challenge failed
                lblStatus.Text = "Captcha Failed!! Please try again!!";
            }
        }
    }
}


(下圖) 在 "我不是機器人" 打勾

(下圖) 依照問題勾選,按下 "驗證" 按鈕


(下圖) Recaptcha 驗證通過了,接下來按下 "Submit" 按鈕

(下圖) 抓到 Recaptcha 驗證目前狀態,顯示在按鈕下方

********************************************************************************

另一個方法,不需要把 GoogleReCaptcha.dll 放到你的方案的 bin 目錄。

Google ReCaptcha 2.0 - ASP.net Control
http://www.codeproject.com/Tips/884193/Google-ReCaptcha-ASP-net-Control

下載 Download source - 202.9 KB

解壓後,GoogleReCaptcha 方案中有兩個專案 SampleReCaptchaApplication 和 GoogleReCaptcha,把 GoogleReCaptcha 專案複製到你自己的方案下


(下圖) Visual Studio 2015 中,在你自己的方案上按下滑鼠右鍵,選 "加入",選 "現有專案"

(下圖) 選 WebApplication4 方案的 GoogleReCaptcha 專案目錄中的 GoogleReCaptcha 檔案

(下圖) 因為下載來的專案是 Visual Studio 2010 建立的,用 Visual Studio 2015 開啟會有警告

(下圖) 成功了

(下圖) 在自己的 WebApplication4 方案下的 WebApplication4 專案下的 "參考" 按下滑鼠右鍵,選 "加入參考"

(下圖) 勾選左邊 "專案" 中的 "方案",右邊把 GoogleReCaptcha 專案打勾,按下 "確定" 按鈕

這樣就不需要把 GoogleReCaptcha.dll 放到你的方案的 bin 目錄。


2016-06-08

[研究] reCAPTCHA 對 Windows 7 + IE9 相容性測試
http://shaurong.blogspot.com/2016/06/recaptcha-windows-7-ie9.html

(完)

相關

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用
http://shaurong.blogspot.com/2016/05/aspnet-webform-c-google-recaptcha.html

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用 (使用 GoogleReCaptcha.dll )
http://shaurong.blogspot.com/2016/05/aspnet-webform-c-google-recaptcha_26.html



沒有留言:

張貼留言