C#.net制作验证码(英文与数字组成的4位随机数),以及MD5值的使用

原文发布时间为:2008-09-22 —— 来源于本人的百度文章 [由搬家工具导入]

参考资料:http://www.cnblogs.com/gwazy/articles/139510.html

三个窗体:

default1:

C#.net制作验证码(英文与数字组成的4位随机数),以及MD5值的使用

属性值:

C#.net制作验证码(英文与数字组成的4位随机数),以及MD5值的使用

源码:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form /></div>
    </form>
</body>
</html>

代码:

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(TextBox1.Text, Session["checkcode"].ToString(),true)==0)
        {
            Response.Redirect("~/Default3.aspx");
        }
    }
}

default2为空窗体:

代码:

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.Design;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Random rd = new Random();
        int number;
        char code;
        string checkcode=string.Empty;
        for (int i = 0; i < 4; i++)
        {
            number = rd.Next();
            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));
            checkcode += code.ToString();
        }
        Session["checkcode"] = checkcode;
        Bitmap bp = new Bitmap(75, 35);
        Graphics g = Graphics.FromImage(bp);
        g.Clear(Color.White);

        for (int i = 0; i < 40; i++)
        {
            int x1 = rd.Next(bp.Width);
            int y1 = rd.Next(bp.Height);
            int x2 = rd.Next(bp.Width);
            int y2 = rd.Next(bp.Height);
            g.DrawLine(new Pen(Color.Aqua), x1, y1, x2, y2);
        }
        Font ft = new Font("Arial", 18, FontStyle.Bold | FontStyle.Italic);
        System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bp.Width, bp.Height), Color.Blue, Color.Blue,10, true);
        g.DrawString(checkcode, ft, lgb, 2, 2);
        for (int i = 0; i < 80; i++)
        {
            int x = rd.Next(bp.Width);
            int y = rd.Next(bp.Height);
            bp.SetPixel(x, y, Color.FromArgb(rd.Next()));
        }
        g.DrawRectangle(new Pen(Color.Silver), 0, 0, bp.Width-1 , bp.Height-1 );
        MemoryStream ms = new MemoryStream();
        bp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        //Response.ClearContent();
        //Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        ms.Dispose();
        ms.Close();
    }
}

default3为空窗体(MD5值的使用):

代码:

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       string md5= System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Session["checkcode"].ToString(),"MD5");
       Response.Write(md5);
    }
}