惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
Help Net Security
Help Net Security
月光博客
月光博客
N
News and Events Feed by Topic
Cloudbric
Cloudbric
博客园 - 司徒正美
L
LangChain Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tenable Blog
The Register - Security
The Register - Security
The Hacker News
The Hacker News
I
InfoQ
The Last Watchdog
The Last Watchdog
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
F
Fortinet All Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
C
Cisco Blogs
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Latest news
Latest news
PCI Perspectives
PCI Perspectives
T
The Blog of Author Tim Ferriss
P
Palo Alto Networks Blog
T
Tor Project blog
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Webroot Blog
Webroot Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - Love Fendi

性能优化系列---查询高cup的sql State模式学习 8.17--8.24积累 sql server 2005 analysis service step by step(三):创建父子维度 sql server 2005 analysis service step by step(二):创建时间维度 sql serve 2005 analysis service step by step(一):创建标准维度 算法练习五:求数组中第k大的数 算法练习四:求N!不溢出 算法练习三:奇偶分割 算法练习二:二分查找 数据库锁 算法练习一:最大公约数与最小公倍数 索引优化 自定义控件中与脚本资源集成的若干处理方式 一条语句删除表中某字段重复的数据 动态按需异步加载js文件 在Nhibernate中使用Json.net中出现Self referencing loop的错误的处理 JQuery学习笔记 c#委托事件 入门
生成验证码,同时异步获取加密后的验证码
Love Fendi · 2009-02-20 · via 博客园 - Love Fendi

今天忙了一天,主要是考虑如何把验证码图片和加密的字符串一同返回,由于上司要求不能用状态来在页面直接传递值,所以只好用异步的了。

验证码的引用页面

<%@ 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>
     <script language="javascript" src="js/jquery-1.2.6.js"></script>
    <script language="javascript" type="text/javascript">
function change()
{
    var img = document.getElementById("Image1");
//var txt=img.getElementById("Text1");
    img.src = img.src + '?';
    $("#md5").load("ValiateCode.aspx", {id:"md5"});

}
function firstload() {
    $("#md5").load("ValiateCode.aspx", { id: "md5" });
}
function test() {
    var hid1 = document.getElementById("hid1");
    if (hid1.value == "0") {
        //如果是第一次加载,则操作
        $("#md5").load("ValiateCode.aspx", { id: "md5" });
    }
    else {
        //否则不动作
    }
}
</script>
</head>
<body onload="test()">
    <form id="form1" runat="server">
    <div id="md5"></div>
    <input type="hidden" id="hid1" runat="server" value="0" />

    <div>
       <img alt="" src="ValiateCode.aspx" id="Image1" runat="server"/><a href="javascript:change();">看不清,换一张</a>
    </div>
    </form>
</body>
</html>

生成验证码的页面

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

<!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 id="form1" runat="server">
    <div>
      
        <div id="result" runat="server"></div>
    </div>
    </form>
</body>
</html>

public partial class ValiateCode : System.Web.UI.Page
{
    public string VNum;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
        }
    }
    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
        if (Request.Form["id"] == null)
        {
            //这里用到session只局限于ValiateCode页面内部

           VNum = this.GenerateNumber(5);
            string md5String = Get_MD5_Method(VNum);
            ChangeTextValue(md5String);
            Session["VNum"] = VNum;
            this.Validate_Code(VNum);
        }

        if (Request.Form["id"] != null)
        {
            //Response.ClearContent();//需要输出图象信息 要修改HTTP头
            VNum = Session["VNum"].ToString();
            Response.Write(Get_MD5_Method(VNum));
            //Request.Form.Clear();
            Response.End();
        }

    }
    public string Get_MD5_Method(string strSource)
    {
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));
        //转换成字符串,并取9到25位15  
        string strResult = BitConverter.ToString(bytResult);
        //转换成字符串,32位17  
        //string strResult = BitConverter.ToString(bytResult); 
        //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉20  
        //strResult = strResult.Replace("-", "");
        return strResult;
    }
    private void ChangeTextValue(string md5String)
    {
        //HtmlInputControl input = (HtmlInputControl)this.FindControl("Text1");
        // input.Value = md5String;
        result.InnerText = md5String;
    }
    private static char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '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 string GenerateNumber(int length)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        Random rd = new Random();
        for (int i = 0; i < length; i++)
        {
            sb.Append(constant[rd.Next(62)]);
        }
        return sb.ToString();
    }

    private void Validate_Code(string Num)
    {
        int Gheight = (int)(Num.Length * 13);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(Gheight, 25);

        Graphics g = Graphics.FromImage(image);
        Rectangle rc = new Rectangle(0, 0, 65, 25);//定义一个矩形

        g.FillRectangle(new SolidBrush(Color.LightYellow), rc);//填充矩形

        g.DrawString(Num, new System.Drawing.Font("Arial", 12), new System.Drawing.SolidBrush(Color.Red), 3, 3);
        //g.FillRectangle(new SolidBrush(new Point(0, 0), new Point(120, 30), Color.FromArgb(0, 0, 0, 0), Color.FromArgb(255, 255, 255, 255)), 0, 0, 120, 30);
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

        Response.ClearContent();//需要输出图象信息 要修改HTTP头
        Response.ContentType = "image/bmp";
        Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();
        Response.End();
    }

}