程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET隨機碼生成示例

ASP.NET隨機碼生成示例

編輯:.NET實例教程

現在很多網頁登陸的時候都使用了隨機圖片的方式,是一種簡單、有效的防止黑客惡意攻擊的手段。今天看了一些網上的資料,明白其生成原理:從樣本中,獲取隨機字符串,隨機字符串保存進session,並以位圖的方式形成隨機碼圖片。

實現:
添加命名空間

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
生成頁代碼

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public partial class getRandImg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //生成隨機碼圖片
        SetValidateCode();
        //生成頁面不保存到cache
        Response.Cache.SetNoStore();
    }

    //設置驗證碼
    private void SetValidateCode()
    {
        //新建位圖
        Bitmap newBitmap = new Bitmap(
                                        71,
                                        23,
                                        PixelFormat.Format32bppArgb
                                     );
        //從位圖獲得繪圖畫面
        Graphics g = Graphics.FromImage(newBitmap);
        //隨機數生成器
        Random r = new Random();
        //繪圖畫面清空
        g.Clear(Color.White);
        //繪圖畫面劃線干擾
        for (int i = 0; i < 50; i++)
        {
            int x1 = r.Next(newBitmap.Width);
   ;         int x2 = r.Next(newBitmap.Width);
            int y1 = r.Next(newBitmap.Height);
            int y2 = r.Next(newBitmap.Height);
            g.DrawLine(new Pen(
                                Color.FromArgb(r.Next())),
                                x1,
                                y1,
                                x2,
                                y2
                              );
        }
        //繪圖畫面點數干擾
        for (int i = 0; i < 100; i++)
        {
            int x = r.Next(newBitmap.Width);
            int y = r.Next(newBitmap.Height);
            newBitmap.SetPixel(
                                x,
                                y,
                                Color.FromArgb(r.Next())
                              );
        }
        //獲得隨機字符串(5位長度)
  &nbsp;     string value = GenerateRandom(5);
        //隨機字符串賦值給Session
        Session["RandCode"] = value;
        //定義圖片顯示字體樣式
        Font font = new Font(
                               "Arial",
                               14,
                               FontStyle.Bold
                            );
        Random rr = new Random();
        int yy = rr.Next(1, 4);
        //定義隨機字符串顯示圖片刷子
        LinearGradientBrush brush = new LinearGradIEntBrush(
                                                              new Rectangle(0, 0, 71, 23),
                                                              Color.Red,
                                                              Color.Blue,
                                                              1.2f, 
            ;                                                  true
                                                           );
        g.DrawString(value, font, brush, 2, yy);
        g.DrawRectangle(new Pen(
                                  Color.Silver),
                                  0,
                                  0,
                                  70,
                                  22
                                );
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        newBitmap.Save(ms, ImageFormat.Gif);
        //輸出圖片
        Response.ClearContent();
        Response.ContentType = "image/gif";
        Response.BinaryWrite(ms.ToArray());
    }

    //常量集
    private static char[] constant ={
                                        '0','1','2','3','4','5','6','7','8','9',
             &nbsp;                          'a','b','c','d','e','f','g','h','i','j',
                                        'k','l','m','n','o','p','q','r','s','t',
                                        'u','v','w','x','y','z'
                                    };


    //生成隨機字符串
    public static string GenerateRandom(int Length)
    {
        System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
        Random rd = new Random();
        for (int i = 0; i < Length; i++)
        {
            newRandom.Append(constant[rd.Next(36)]);
        }
        return newRandom.ToString();
    }
}

使用隨機圖片的頁面,IMAGE控件的寫法如下:

<asp:Image ID="Image1" ImageUrl="~/getRandImg.ASPx" runat="server" />

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved