首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

用 C# (ASP.NET) 寫出登入驗證碼

     編輯:關於C#

驗證碼生成器完整代碼

繼承 System.Web.UI.Page,Page_Load 生成圖片:

public class ValidateCode : System.Web.UI.Page {
    private void Page_Load(object sender, EventArgs e) {
        this.CreateCheckCodeImage(GenerateCheckCode());
    }

    private string GenerateCheckCode() {
        string checkCode = String.Empty;
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            int number = random.Next();
            char code = (number % 2 == 0) ? (char)('0' + (char)(number % 10)) : (char)('A' + (char)(number % 26));
            checkCode += code.ToString();
        }
        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
        return checkCode;
    }

    private void CreateCheckCodeImage(string checkCode) {
        if (string.IsNullOrEmpty(checkCode)) return;
        Bitmap image = new Bitmap((int)Math.Ceiling(checkCode.Length * 12.5), 22);
        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), x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height), y2 = random.Next(image.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }
            // 繪製驗證碼文字
            Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
            LinearGradientBrush brush = new 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 < 100; i++) {
                int x = random.Next(image.Width), 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);
            // 輸出 GIF
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        } finally {
            g.Dispose(); image.Dispose();
        }
    }
}

前端使用方式

頁面名為 CheckCode.aspx,登入頁面用 <IMG> 顯示:

<IMG src="CheckCode.aspx">

登入按鈕驗證邏輯

private void btnLogin_Click(object sender, ImageClickEventArgs e) {
    if (Request.Cookies["CheckCode"] == null) {
        lblMessage.Text = "您的瀏覽器已禁用 Cookies,請啟用後再使用本系統。";
        lblMessage.Visible = true; return;
    }
    if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0) {
        lblMessage.Text = "驗證碼錯誤,請輸入正確的驗證碼。";
        lblMessage.Visible = true; return;
    }
    // ... 其他登入邏輯
}

採用 Cookie 儲存驗證碼,不區分大小寫比對。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved