顯示具有 CAPTCHA 標籤的文章。 顯示所有文章
顯示具有 CAPTCHA 標籤的文章。 顯示所有文章

2025年10月13日 星期一

[研究]reCAPTCHA 的服務條款將有異動。採取行動。

[研究]reCAPTCHA 的服務條款將有異動。採取行動。

2025-10-13

Google reCAPTCHA 的服務條款將有異動,點「採取行動」跳到下面網址

https://www.google.com/recaptcha/admin/migrate



ChatGPT

Google 正在進行 reCAPTCHA 服務的重大變革,預計於 2025 年底前完成。如果您目前透過舊版 reCAPTCHA Admin Console 管理網站的驗證金鑰,建議您儘早採取行動,以免影響網站功能。

⚠️ 主要變動內容
1. 強制遷移至 Google Cloud 項目

Google 將所有 reCAPTCHA 服務整合至 Google Cloud 平台,舊版管理介面將於 2025 年底停止運作。所有現有的 reCAPTCHA 金鑰(包括 v2 和 v3)必須遷移至 Google Cloud 項目。雖然 Google 會在年底前自動完成遷移,但建議您主動進行,以確保網站功能不中斷。

2. 新的計費模式

遷移後,Google Cloud 提供每月最多 10,000 次免費驗證(assessments)。若超過此限制,將依照 Google Cloud 的計費標準收費。詳細資訊請參閱 Google Cloud reCAPTCHA 文件

3. 新功能與增強的安全性

遷移至 Google Cloud 後,您將能夠使用如帳戶防禦、密碼防禦、多因素驗證等進階功能,並透過 Google Cloud Console 進行更精細的監控與管理。

❓ 不遷移會怎樣?

若您未在 2025 年底前完成遷移,可能會面臨以下風險:
  • 服務中斷:網站上的 reCAPTCHA 功能可能會停止運作,影響用戶體驗。
  • 無法使用新功能:無法享受 Google Cloud 提供的進階安全功能。
  • 可能產生額外費用:若超過免費配額,可能需要支付額外費用。

✅ 如何遷移?

您可以透過以下方式遷移:

1.使用 reCAPTCHA 管理控制台
2.使用 Google Cloud Console:
  • 在 Google Cloud Console ( https://console.cloud.google.com/?pli=1 )  中,選擇或建立一個項目。
  • 啟用 reCAPTCHA Enterprise API。
  • 在「Classic keys」區域,選擇您要遷移的金鑰,點擊「升級金鑰」,並確認遷移。
遷移過程不需要修改網站程式碼,且通常在 5–10 分鐘內完成。

🛠️ 需要協助嗎?

若您使用 WordPress 等平台,或有自訂的 reCAPTCHA 實作,可能需要進一步的協助。您可以參考以下資源:

如何將現有的 reCAPTCHA 金鑰遷移至 reCAPTCHA Enterprise

如何從 reCAPTCHA Classic 遷移至 Google Cloud

為了讓您更了解遷移過程,以下是一段官方的教學影片:

How to migrate existing reCAPTCHA keys to reCAPTCHA Enterprise

(完)

2024年7月12日 星期五

[研究]圖形驗證(Captcha)、身分證、IP 算是 多因子驗證 MFA 的 因子嗎?

[研究]圖形驗證(Captcha)、身分證、IP  算是 多因子驗證 MFA 的 因子嗎?

2024-07-12、2024-07-18

多因子驗證/多因素驗證 (Multi-Factor Authentication, MFA)三種身分驗證類型

( Something you know, Something you have, Something you are )

( 所知之事、所有之物、所具之形)

( 所知因子、持有因子、生物因子)

至少包含2種。


註:雙因子驗證/雙因素驗證 (Two-Factor Authentication, TFA) 是剛好使用兩種因子。

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

NIST Special Publication 800-63-3 Digital Identity Guidelines

https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-63-3.pdf

The classic paradigm for authentication systems identifies three factors as the cornerstones of authentication:

  • Something you know (e.g., a password).
  • Something you have (e.g., an ID badge or a cryptographic key).
  • Something you are (e.g., a fingerprint or other biometric data).

MFA refers to the use of more than one of the above factors. 

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

多重要素驗證的趨勢發展與分析 - 財團法人台灣網路資訊中心部落格 | TWNIC Blog

https://blog.twnic.tw/2022/12/20/25018/ 

目前主要的MFA身份驗證類型分別為:

  • 知識資訊(Something you know):例如使用者名稱、密碼
  • 持有資訊(Something you have):例如獨立的裝置、身分證、提款
  • 生物資訊(Something you are):例如臉部辨識、指紋辨識
  • 位置資訊(Somewhere you are):例如IP位址或地理資訊

MFA需要符合兩種以上「不同類型」的驗證方法,假設使用指紋辨識與臉部辨識登入,則因為皆屬於「生物資訊」而不符合MFA,最熟悉的方式莫過於在ATM提款時需要放入銀行卡片(Things you have)與輸入提款密碼(Things you know)。

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

圖形驗證(Captcha)  算是 多因素驗證(Multi-Factor Authentication, MFA) 的 所知資訊(Something you know)嗎 ?

ChatGPT

圖形驗證(Captcha)通常不被認為是多因素驗證(MFA)中的「Something you know」因素。這兩者在身份驗證安全性上有所不同:

  1. 圖形驗證(Captcha):這是一種用於區分人類和機器的技術,通常通過要求用戶識別圖片中的文字或圖形來進行驗證。Captcha確保正在進行操作的是真人而非自動化程式(如機器人)。
  2. 多因素驗證(MFA):MFA 是一種安全驗證方法,通常包括以下幾種因素之一或多種因素的結合:

    • Something you know(你所知道的):如密碼、PIN碼。
    • Something you have(你所擁有的):如智能卡、USB安全鍵、手機。
    • Something you are(你所是的):如生物特徵識別,例如指紋、視網膜掃描等。

Captcha主要是用來證明使用者是真人,並不涉及用戶特定的秘密信息或知識(something you know),因此它不屬於MFA中「Something you know」這個因素。

總結來說,Captcha和MFA都是用於增強身份驗證的安全性,但是它們解決的是不同的安全問題,並在驗證過程中使用不同類型的信息。

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

身分證號碼  算是 多因素驗證(Multi-Factor Authentication, MFA) 的 所知資訊(Something you know)嗎 ?

ChatGPT

身分證號碼通常被視為多因素驗證(MFA)中的「Something you know」因素的一部分,但這要視具體情況而定。

在某些情況下,身分證號碼確實可以被視為「Something you know」,因為它是一種具有唯一性和私密性的個人識別信息,只有合法的使用者才應該知道自己的身分證號碼。在這種情況下,當用戶輸入正確的身分證號碼時,可以作為驗證其身份的一部分。

然而,需要注意的是,單獨的身分證號碼可能不足以提供足夠的安全性,因此通常會將其與其他因素結合,例如密碼、PIN碼、生物特徵等,來實現更強大的多因素驗證(MFA)機制。

總結來說,身分證號碼可以被視為MFA中「Something you know」的一部分,但在實際應用中,建議與其他驗證因素結合,以提升安全性。

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

身分證號碼  算是 多因素驗證(Multi-Factor Authentication, MFA) 的 Something you have(你所擁有的)嗎 ?

ChatGPT

身分證號碼通常不算是多因素驗證(MFA)中的「Something you have」(你所擁有的)因素。

「Something you have」通常指的是具體的物理或虛擬物件,例如:

  • 智能卡
  • USB安全鍵
  • 手機或其他設備
  • 硬件驗證器(如硬體令牌)

這些物件是實體存在的,並且在驗證過程中可以被用戶控制和擁有。身分證號碼雖然是一種識別信息,但它本身不是用戶擁有或控制的具體物件,因此不符合「Something you have」這一因素的定義。

身分證號碼更適合作為「Something you know」(你所知道的)的一部分,因為它是用戶所知道的私密信息之一。

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

多重要素驗證 - 維基百科,自由的百科全書

https://zh.wikipedia.org/zh-tw/%E5%A4%9A%E9%87%8D%E8%A6%81%E7%B4%A0%E9%A9%97%E8%AD%89

  • 知識要素:例如使用者名稱及密碼
  • 持有的物件:例如獨立的裝置、安全權杖、身分證、提款卡等等
  • 原生要素:生物特徵:例如臉部辨識、指紋辨識、語音辨識

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

身分證卡片  算是 多因素驗證(Multi-Factor Authentication, MFA) 的 Something you have(你所擁有的)嗎 ?

ChatGPT

身分證卡片通常可以被視為多因素驗證(MFA)中的「Something you have」(你所擁有的)因素。

多因素驗證(MFA)通常涉及以下幾種因素之一或多個因素的組合來確認使用者身份:

  • Something you know(你所知道的):例如密碼、PIN碼、安全問題的答案等。
  • Something you have(你所擁有的):例如智能卡、USB安全鍵、手機或身分證卡片等。
  • Something you are(你所是的):例如生物特徵識別,如指紋、視網膜掃描等。

身分證卡片作為一種具體的物理證件,可以被用戶擁有和控制,並且通常是由官方機構發放,具有一定的唯一性和可信度。因此,當身分證卡片用於身份驗證時,它可以被歸類為MFA中的「Something you have」因素。

總結來說,身分證卡片作為一種實體物件,通常能夠有效增強身份驗證的安全性,因此在MFA策略中常常被使用。

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

有讀卡機可以讀取身分證卡片中的身分證號碼嗎 ?

ChatGPT

是的,有讀卡機可以讀取台灣的身分證卡片中的身分證號碼。這些讀卡機可以透過內建的軟體來讀取內含的資訊,包括身分證號碼、姓名、生日等個人資料。通常使用這些讀卡機需要搭配特定的應用軟體,並且用戶可能需要授權才能讀取和使用這些資料。

如果你有任何特定的需求或問題,歡迎告訴我,我可以幫助你找到更多相關資訊。

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

自然人憑證讀卡機可以讀取台灣身分證卡片中的身分證號碼嗎 ?

ChatGPT

自然人憑證讀卡機主要設計用來讀取自然人憑證,這是一種含有數位憑證的智慧卡。台灣的身分證卡片(新版的 IC 身分證)也使用類似的智慧卡技術,但自然人憑證讀卡機能否讀取台灣身分證卡片中的身分證號碼,取決於讀卡機的軟體是否支援此功能。

大多數自然人憑證讀卡機硬體上應該是可以讀取身分證卡片的,但是你可能需要相應的軟體來解碼並提取身分證中的資料。一般來說,專門設計的 IC 身分證讀卡機和配套軟體會更適合這個用途。

簡而言之,自然人憑證讀卡機理論上可以讀取台灣身分證卡片中的身分證號碼,但需要確保你有相應的軟體支援和授權。

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

手機號碼驗證碼

登入時,輸入帳號密碼後,系統根據帳號密碼從資料庫找出該使用者手機號碼,寄出一個驗證碼到該手機號碼,於網頁上請使用者輸入驗證通過後才能登入成功,此手機號碼驗證碼算是MFA的 Something you have(你所擁有的) 嗎 ? Something you know(你所知道的)嗎 ? 不算 ?

ChatGPT

手機號碼驗證碼屬於多因素驗證(MFA)中的 "Something you have"(你所擁有的)。以下是詳細的解釋:

  • Something you know(你所知道的):這類包括密碼、PIN 碼等,是使用者頭腦中記住的信息。
  • Something you have(你所擁有的):這類包括實體令牌、智慧卡、手機等設備。手機驗證碼屬於這一類,因為它需要你擁有並能接收短信的手機。
  • Something you are(你所代表的):這類包括生物特徵識別,如指紋、面部識別、虹膜掃描等。

因此,當系統發送驗證碼到手機號碼並要求輸入該驗證碼時,這個驗證碼代表了 "Something you have"。

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

Email驗證碼

登入時,輸入帳號密碼後,系統根據帳號、密碼從資料庫找出該使用者Email,寄出一個驗證碼到該Email,於網頁上請使用者輸入驗證通過後才能登入成功,此Email中的驗證碼算是MFA的 Something you have(你所擁有的) 嗎 ? Something you know(你所知道的)嗎 ? 不算 ?

ChatGPT

當系統發送驗證碼到電子郵件並要求輸入該驗證碼時,這個驗證碼的類別有點複雜。根據多因素驗證(MFA)的分類:

  • Something you know(你所知道的):這類包括密碼、PIN 碼等,是使用者頭腦中記住的信息。
  • Something you have(你所擁有的):這類包括實體令牌、智慧卡、手機等設備。某些情況下,訪問電子郵件帳戶也可以被視為 "Something you have"。
  • Something you are(你所代表的):這類包括生物特徵識別,如指紋、面部識別、虹膜掃描等。

在這種情況下,電子郵件驗證碼既不完全屬於 "Something you know"(你所知道的),也不完全屬於 "Something you have"(你所擁有的)。它更傾向於 "Something you have" 的概念,因為它涉及訪問一個特定的電子郵件帳戶,就像訪問手機接收短信一樣。然而,這種分類在某些情況下可能會有所爭議,因為電子郵件本身是通過知道密碼來訪問的。

總結來說,電子郵件驗證碼更接近於 "Something you have"。

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

系統的來源 IP 限制

系統的來源 IP 限制,算是多因素驗證(MFA)的一種因子嗎? Something you have(你所擁有的) 嗎 ? Something you know(你所知道的)嗎 ? 不算 ?

ChatGPT

系統的來源 IP 限制通常不被視為多因素驗證(MFA)的一種因子。以下是原因:

  • Something you know(你所知道的):IP 限制是基於系統設置,用戶不需要特別知道或記住任何額外信息,因此不符合這個分類。
  • Something you have(你所擁有的):這通常指實體設備、智慧卡、手機等,用戶必須擁有或可以控制這些因子。

IP 限制屬於一種訪問控制的技術,而非用戶身份驗證的因子。它通常用於增加安全性,限制從特定位置或網路訪問資源的範圍,但並不涉及用戶個人身份的驗證。

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

OTP(一次性密碼,One Time Password)設備,算是 MFA 因子?哪一種?

ChatGPT

OTP(一次性密碼,One Time Password)設備確實是多因素驗證(MFA)的一種因子。它屬於以下類別:

Something you have(你所擁有的):OTP 設備是一個實體設備(如硬體令牌或手機上的 OTP 應用程式),使用者必須擁有這個設備來生成或接收一次性密碼。

總結來說,OTP 設備是多因素驗證中的 "Something you have(你所擁有的)" 因子。

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

如果根據使用者登入 AD 網域,由 DHCP 配給非固定 IP,算 MFA 的因子?哪一種?

如果根據 帳號 配給 固定 IP,算 MFA 的因子?哪一種?

如果根據 網路卡 MAC Address 配給 固定 IP,算 MFA 的因子?哪一種? 

ChatGPT

關於 IP 和 MAC 地址在多因素驗證(MFA)中的角色,可以進一步分析如下:

  • 根據使用者登入 AD 網域,由 DHCP 配給非固定 IP:這並不算是多因素驗證的一部分。DHCP 配給非固定 IP 是一種網絡管理技術,並不涉及用戶身份驗證。
  • 根據帳號配給固定 IP:這種方法也不算是 MFA 的一個因子。配給固定 IP 是基於帳號的網絡配置方式,並不是一種用戶身份驗證因子。
  • 根據網絡卡 MAC 地址配給固定 IP:這種方法可以被視為一種設備識別技術,但也不算作 MFA 的一個因子。雖然 MAC 地址可以唯一標識一個設備,但它不是傳統意義上的用戶驗證因子。

總結來說,這些方法主要是網絡管理和訪問控制技術,這些技術主要是用於增加安全性和控制訪問,而不是用戶身份驗證的一部分。

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

(完)

2023年4月13日 星期四

[研究]Google reCAPTCHA請稍後再試

[研究]Google reCAPTCHA請稍後再試

2023-04-13 



(下圖)再回答多次九宮格問題後, 出現

請稍後再試

您的電腦或網路可能正在傳送自動查詢要求。為保護使用者,我們暫時無法處理您的要求。詳情請參閱我們的說明頁面。

我們的說明頁面
https://developers.google.com/recaptcha/docs/faq?hl=zh-tw#my-computer-or-network-may-be-sending-automated-queries

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

使用者還真是多啊。

(完)

2022年4月14日 星期四

[研究][ASP.NET]WebForm圖形驗證碼(CAPTCHA)程式(二)

[研究][ASP.NET]WebForm圖形驗證碼(CAPTCHA)程式(二)

2022-04-14

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

一般常用的 Google reCAPTCHA需要Web Server和Client可以連上Internet,但是若因特殊需求,就不能用 Google reCAPTCHA,需要一個封閉區域網路上可用的。

本篇參考這篇,只是該篇沒有完整可測試範例,本篇進行實際測試。

Simple CAPTCHA, Create Your Own in C# - CodeProject
4 Aug 2010
https://www.codeproject.com/Articles/99148/Simple-CAPTCHA-Create-Your-Own-in-Csharp

Captcha.ashx.cs

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Web;
using System.Web.SessionState;

namespace WebApplication1
{
    /// <summary>
    /// Captcha 的摘要描述
    /// </summary>
    public class Captcha : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            int iHeight = 80;
            int iWidth = 190;
            Random oRandom = new Random();

            int[] aBackgroundNoiseColor = new int[] { 150, 150, 150 };
            int[] aTextColor = new int[] { 0, 0, 0 };
            int[] aFontEmSizes = new int[] { 15, 20, 25, 30, 35 };

            string[] aFontNames = new string[]
            {
                "Comic Sans MS",
                "Arial",
                "Times New Roman",
                "Georgia",
                "Verdana",
                "Geneva"
            };
            FontStyle[] aFontStyles = new FontStyle[]
            {
                FontStyle.Bold,
                FontStyle.Italic,
                FontStyle.Regular,
                FontStyle.Strikeout,
                FontStyle.Underline
            };
            HatchStyle[] aHatchStyles = new HatchStyle[]
            {
                HatchStyle.BackwardDiagonal, HatchStyle.Cross,
                   HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal,
                HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,
                   HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross,
                HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid,
                   HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,
                HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard,
                   HatchStyle.LargeConfetti, HatchStyle.LargeGrid,
                HatchStyle.LightDownwardDiagonal, HatchStyle.LightHorizontal,
                   HatchStyle.LightUpwardDiagonal, HatchStyle.LightVertical,
                HatchStyle.Max, HatchStyle.Min, HatchStyle.NarrowHorizontal,
                   HatchStyle.NarrowVertical, HatchStyle.OutlinedDiamond,
                HatchStyle.Plaid, HatchStyle.Shingle, HatchStyle.SmallCheckerBoard,
                   HatchStyle.SmallConfetti, HatchStyle.SmallGrid,
                HatchStyle.SolidDiamond, HatchStyle.Sphere, HatchStyle.Trellis,
                   HatchStyle.Vertical, HatchStyle.Wave, HatchStyle.Weave,
                HatchStyle.WideDownwardDiagonal, HatchStyle.WideUpwardDiagonal, HatchStyle.ZigZag
            };

            //Get Captcha in Session
            string sCaptchaText = context.Session["Captcha"].ToString();

            //Creates an output Bitmap
            Bitmap oOutputBitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
            Graphics oGraphics = Graphics.FromImage(oOutputBitmap);
            oGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            //Create a Drawing area
            RectangleF oRectangleF = new RectangleF(0, 0, iWidth, iHeight);
            Brush oBrush = default(Brush);

            //Draw background (Lighter colors RGB 100 to 255)
            oBrush = new HatchBrush(aHatchStyles[oRandom.Next
                (aHatchStyles.Length - 1)], Color.FromArgb((oRandom.Next(100, 255)),
                (oRandom.Next(100, 255)), (oRandom.Next(100, 255))), Color.White);
            oGraphics.FillRectangle(oBrush, oRectangleF);

            System.Drawing.Drawing2D.Matrix oMatrix = new System.Drawing.Drawing2D.Matrix();
            int i = 0;
            for (i = 0; i <= sCaptchaText.Length - 1; i++)
            {
                oMatrix.Reset();
                int iChars = sCaptchaText.Length;
                int x = iWidth / (iChars + 1) * i;
                int y = iHeight / 2;

                //Rotate text Random
                oMatrix.RotateAt(oRandom.Next(-40, 40), new PointF(x, y));
                oGraphics.Transform = oMatrix;

                //Draw the letters with Random Font Type, Size and Color
                oGraphics.DrawString
                (
                //Text
                sCaptchaText.Substring(i, 1),
                //Random Font Name and Style
                new Font(aFontNames[oRandom.Next(aFontNames.Length - 1)],
                   aFontEmSizes[oRandom.Next(aFontEmSizes.Length - 1)],
                   aFontStyles[oRandom.Next(aFontStyles.Length - 1)]),
                //Random Color (Darker colors RGB 0 to 100)
                new SolidBrush(Color.FromArgb(oRandom.Next(0, 100),
                   oRandom.Next(0, 100), oRandom.Next(0, 100))),
                x,
                oRandom.Next(10, 40)
                );
                oGraphics.ResetTransform();
            }

            MemoryStream oMemoryStream = new MemoryStream();
            oOutputBitmap.Save(oMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
            byte[] oBytes = oMemoryStream.GetBuffer();

            oOutputBitmap.Dispose();
            oMemoryStream.Close();

            context.Response.BinaryWrite(oBytes);
            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!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>
            <asp:TextBox ID="txtCaptcha" runat="server" AutoComplete="Off"></asp:TextBox>
            <br />
            <asp:Image ID="imgCaptcha" ImageUrl="Captcha.ashx" runat="server" />
            <br />
            <asp:Button ID="Button_Check" runat="server" Text="Check" 
                OnClick="Button_Check_Click" />
            <asp:Button ID="Button_Refresh" runat="server" Text="Refresh" 
                OnClick="Button_Refresh_Click" />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "";
            if (!IsPostBack)
            {
                SetCaptchaText();
            }
        }

        private void SetCaptchaText()
        {
            Random oRandom = new Random();
            //int iNumber = oRandom.Next(100000, 999999);
            int iNumber = oRandom.Next(1000, 9999);
            Session["Captcha"] = iNumber.ToString();
        }

        protected void Button_Check_Click(object sender, EventArgs e)
        {
            if (Session["Captcha"].ToString() != txtCaptcha.Text.Trim())
            {
                //Response.Redirect("Failed.aspx");
                Label1.Text = "Failed";
                SetCaptchaText();
            }
            else
            {
                //Response.Redirect("Success.aspx");
                Label1.Text = "Success";
            }
        }

        protected void Button_Refresh_Click(object sender, EventArgs e)
        {
            SetCaptchaText();
        }
    }
}

執行結果

(完)

相關

[研究][ASP.NET]WebForm圖形驗證碼(CAPTCHA)程式(二)
https://shaurong.blogspot.com/2022/04/aspnetwebformcaptcha_14.html

[研究][ASP.NET]WebForm圖形驗證碼(CAPTCHA)程式(一)
http://shaurong.blogspot.com/2022/04/aspnetwebformcaptcha.html

[研究]ASP.NET WebForm CAPTCHA圖形驗證碼
https://shaurong.blogspot.com/2021/12/aspnet-webform-captcha.html

Simple CAPTCHA, Create Your Own in C# - CodeProject
4 Aug 2010
https://www.codeproject.com/Articles/99148/Simple-CAPTCHA-Create-Your-Own-in-Csharp
(無完整範例,但可用)

A CAPTCHA Server Control for ASP.NET - CodeProject
2007/02/01
https://www.codeproject.com/Articles/8751/A-CAPTCHA-Server-Control-for-ASP-NET
( 程式為 VB + .NET Framework 2.0,要變更專案的屬性為 .NET Framewok 4.8,但執行仍失敗)

A Simple CAPTCHA Image Verification in C# and ASP.Net
2009/02/16
http://www.codedigest.com/CodeDigest/88-A-Simple-CAPTCHA-Image-Verification-in-C--and-ASP-Net.aspx

使用 CAPTCHA 防止 bot 使用您的 ASP.NET Web Razor) 網站
https://docs.microsoft.com/zh-tw/aspnet/web-pages/overview/security/using-a-catpcha-to-prevent-automated-programs-bots-from-using-your-aspnet-web-site


2022年4月4日 星期一

[研究][ASP.NET]WebForm圖形驗證碼(CAPTCHA)程式

[研究][ASP.NET]WebForm圖形驗證碼(CAPTCHA)程式

2022-04-04

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

一般常用的 Google reCAPTCHA需要Web Server和Client可以連上Internet,但是若因特殊需求,就不能用 Google reCAPTCHA,需要一個封閉區域網路上可用的。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!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>
            驗證碼<asp:TextBox AutoComplete="Off" ID="txtCheckCode" Width="60" 
                runat="server"></asp:TextBox>
            <img src="/GenerateCaptchaCodeImage.aspx" width="69" height="20" />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;

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

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.Compare(Session["CheckCode"].ToString(), txtCheckCode.Text, true) != 0)
            {
                Response.Write("<script>alert('圖片驗證碼錯誤');</script>");
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "圖片驗證碼錯誤";
                return;
            }
            else
            {
                Response.Write("<script>alert('圖片驗證碼成功');</script>");
                Label1.ForeColor = System.Drawing.Color.Green;
                Label1.Text = "圖片驗證碼成功";
            }
        }
    }
}


GenerateCaptchaCodeImage.aspx
<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="GenerateCaptchaCodeImage.aspx.cs" 
    Inherits="WebApplication1.GenerateCaptchaCodeImage" %>

<!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>
        </div>
    </form>
</body>
</html>


GenerateCaptchaCodeImage.aspx.cs
using System;
using System.Drawing;
using System.Web;

namespace WebApplication1
{
    public partial class GenerateCaptchaCodeImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CreateCheckCodeImage(GenerateCheckCode());
        }
        private string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = string.Empty;

            System.Random random = new Random();

            for (int i = 0; i < 4; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));

                checkCode += code.ToString();
            }

            Session["CheckCode"] = checkCode;
            Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

            return checkCode;
        }

        private void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == string.Empty)
                return;

            //System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            //System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 20)), 40);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(100, 30);

            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成隨機生成器
                Random random = new Random();

                //清空圖片背景色
                g.Clear(Color.White);

                //畫圖片的背景噪音線
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new System.Drawing.Font("Arial", 22, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                //g.DrawString(checkCode, font, brush, 2, 2);
                g.DrawString(checkCode, font, brush, 2, 2);

                //畫圖片的前景噪音點
                for (int i = 0; i < 500; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //畫圖片的邊框線
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}


(完)

相關

浮雲雅築: [研究]ASP.NET WebForm CAPTCHA圖形驗證碼
https://shaurong.blogspot.com/2021/12/aspnet-webform-captcha.html

Simple CAPTCHA, Create Your Own in C# - CodeProject
https://www.codeproject.com/Articles/99148/Simple-CAPTCHA-Create-Your-Own-in-Csharp

A CAPTCHA Server Control for ASP.NET - CodeProject
https://www.codeproject.com/Articles/8751/A-CAPTCHA-Server-Control-for-ASP-NET

A Simple CAPTCHA Image Verification in C# and ASP.Net
http://www.codedigest.com/CodeDigest/88-A-Simple-CAPTCHA-Image-Verification-in-C--and-ASP-Net.aspx

使用 CAPTCHA 防止 bot 使用您的 ASP.NET Web Razor) 網站
https://docs.microsoft.com/zh-tw/aspnet/web-pages/overview/security/using-a-catpcha-to-prevent-automated-programs-bots-from-using-your-aspnet-web-site


2021年12月6日 星期一

[研究]ASP.NET WebForm CAPTCHA圖形驗證碼

[研究]ASP.NET WebForm CAPTCHA圖形驗證碼

2021-11-19

Visual Studio 2022 + C# + ASP.NET + WebForm

參考

https://dotblogs.com.tw/shadow/2011/10/05/38823

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CAPTCHATest.Default" %>

<!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>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <img src="ValidateNumber.ashx" alt="驗證碼" name="imgCode" />
            <input type="button" onclick="form1.imgCode.src='ValidateNumber.ashx?' + Math.random();" value="重新整理" />
            <br />
            <asp:Button Text="送出" ID="Button1" runat="server" OnClick="btn_submit_Click" /><br />
            <asp:Label ID="Label1" name="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>


Default.aspx.cs


using System;

namespace CAPTCHATest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "";
        }
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string captcheValidateNumber = Session["ValidateNumber"].ToString();
            if (TextBox1.Text.Trim() == captcheValidateNumber)
            {
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "OK";
            }
            else
            {
                Label1.ForeColor = System.Drawing.Color.Green;
                Label1.Text = "Fail";
            }
        }

    }
}


ValidateNumber.ashx


using System;
using System.Drawing;
using System.Web;
using System.Web.SessionState;

namespace CAPTCHATest
{
    /// <summary>
    /// ValidateNumber 的摘要描述
    /// </summary>
    public class ValidateNumber : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {

            int NumCount = 5;//預設產生5位亂數
            if (!string.IsNullOrEmpty(context.Request.QueryString["NumCount"]))
            {//有指定產生幾位數

                //字串轉數字,轉型成功的話儲存到 NumCount,不成功的話,NumCount會是0
                Int32.TryParse(context.Request.QueryString["NumCount"].Replace("'", "''"), out NumCount);
            }
            if (NumCount == 0) NumCount = 5;
            //取得亂數
            var rand = new Random();
            // 產生4位數字,1000~9999
            string str_ValidateCode = rand.Next(1000, 9999).ToString();
            /*用於驗證的Session*/
            context.Session["ValidateNumber"] = str_ValidateCode;

            //取得圖片物件
            System.Drawing.Image image = this.CreateCheckCodeImage(context, str_ValidateCode);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            /*輸出圖片*/
            context.Response.Clear();
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(ms.ToArray());
            ms.Close();

        }

        #region 產生圖片
        private System.Drawing.Image CreateCheckCodeImage(HttpContext context, string checkCode)
        {

            System.Drawing.Bitmap image = new System.Drawing.Bitmap((checkCode.Length * 20), 30);//產生圖片,寬20*位數,高30像素
            System.Drawing.Graphics g = Graphics.FromImage(image);


            //生成隨機生成器
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int int_Red = 0;
            int int_Green = 0;
            int int_Blue = 0;
            int_Red = random.Next(256);//產生0~255
            int_Green = random.Next(256);//產生0~255
            int_Blue = (int_Red + int_Green > 400 ? 0 : 400 - int_Red - int_Green);
            int_Blue = (int_Blue > 255 ? 255 : int_Blue);

            //清空圖片背景色
            g.Clear(Color.FromArgb(int_Red, int_Green, int_Blue));

            //畫圖片的背景噪音線
            for (int i = 0; i <= 24; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

                g.DrawEllipse(new Pen(Color.DarkViolet), new System.Drawing.Rectangle(x1, y1, x2, y2));
            }

            Font font = new System.Drawing.Font("Arial", 20, (System.Drawing.FontStyle.Bold));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);

            g.DrawString(checkCode, font, brush, 2, 2);
            for (int i = 0; i <= 99; i++)
            {

                //畫圖片的前景噪音點
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //畫圖片的邊框線
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);


            return image;

        }
        #endregion

        /*實作 IHttpHandler介面的方法,不可刪除*/
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


(完)

2016年5月25日 星期三

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用

2016-05-25

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

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

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



在 .aspx 檔案中 (ex : :Login.aspx )

在 <head> 和  </head> 之間,</head> 之前插入

<script src='https://www.google.com/recaptcha/api.js'></script>


繁體中文用戶,可考慮改用

<script src='https://www.google.com/recaptcha/api.js?hl=zh-TW'></script>


在 <form> 和  </form> 之間,</form> 之前插入

<div class="g-recaptcha" data-sitekey="你的 Site Key"></div>


這邊有些官方資訊可以參考
https://developers.google.com/recaptcha/docs/display#auto_render
https://developers.google.com/recaptcha/docs/verify#api-request

但是 Server端的驗證,官方的資訊看的有點頭大,所以另外找參考資料

How to Validate Recaptcha V2 Server side
http://www.codeproject.com/Tips/851004/How-to-Validate-Recaptcha-V-Server-side

在 Visual Studio 2015 測試成功
Default.aspx 內容如下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>
<!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>
    <script type="text/javascript" src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="g-recaptcha" data-sitekey="換上你的 Site Key "></div>
    </div>
        <asp:Button ID="btnLogin" runat="server" Text="Check Recaptcha" OnClick="btnLogin_Click"  TabIndex ="4"/>
    </form>
    <asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
</body>
</html>



在 <form> 和  </form> 之間,</form> 之前插入

using System;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;

namespace WebApplication2
{
    public class MyObject
    {
        public string success { get; set; }
    }
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Validate())
            {
                lblmsg.Text = "Valid Recaptcha";
                lblmsg.ForeColor = System.Drawing.Color.Green;
            }

            else
            {
                lblmsg.Text = "Not Valid Recaptcha";
                lblmsg.ForeColor = System.Drawing.Color.Red;
            }
        }
            public bool Validate()
        {
            string Response = Request["g-recaptcha-response"];//Getting Response String Append to Post Method
            bool Valid = false;
            //Request to Google Server
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create
            (" https://www.google.com/recaptcha/api/siteverify?secret=換上你的 Secret Key&response=" + Response);
            try
            {
                //Google recaptcha Response
                using (WebResponse wResponse = req.GetResponse())
                {

                    using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
                    {
                        string jsonResponse = readStream.ReadToEnd();

                        JavaScriptSerializer js = new JavaScriptSerializer();
                        MyObject data = js.Deserialize<MyObject>(jsonResponse);// Deserialize Json

                        Valid = Convert.ToBoolean(data.success);
                    }
                }

                return Valid;
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }
    }
}


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

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

(下圖) Recaptcha 驗證通過了,接下來按下 "Check Recaptcha" 按鈕
 (下圖) 按下 "Check Recaptcha" 按鈕可以抓到 Recaptcha 驗證目前狀態,顯示在按鈕下方

(完)

相關

[研究] [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

[研究] Google reCAPTCHA 驗證程序已到期,請再次勾選核取方塊。

[研究][ASP.NET] Captcha Failed!! Please try again!!

[研究] Google reCaptcha 在 Firewall 要開放的 IP

[研究] Google reCAPTCHA 出現錯誤:網站金鑰的網域無效
[研究] reCAPTCHA 對 Windows 7 + IE8, IE9, IE10, IE11 相容性測試

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

[研究] Google reCAPTCHA 教學使用

[研究] [ASP.NET WebForm C#] Google reCAPTCHA 試用