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


(完)

沒有留言:

張貼留言