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

推荐订阅源

Forbes - Security
Forbes - Security
A
Arctic Wolf
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
A
About on SuperTechFans
P
Palo Alto Networks Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
T
Tor Project blog
IT之家
IT之家
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
N
News and Events Feed by Topic
The Cloudflare Blog
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LangChain Blog
I
InfoQ
F
Full Disclosure
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
V
V2EX

博客园 - xixi8820

输入框验证输入数字的js 关于VS2005中GridView的自定义分页,单选、多选、排序、自增列的简单应用(转载的) 使用t-sql从身份证号中提取生日(转自别人,个人学习收藏用) 一个最简单的登录例子 js中setTimeout与setInterval的区别 js将html中的内容导出word、或者excel文件的方法 javascript 获得指定日期的临近日期的方法 在Asp.Net中应用DataFormatString ASP.NET中App_Code,App_Data等文件夹的作用 javascript 客户端验证 在DataGrid中模版列显示图片 上传图片 页面取物理路径和几种获取asp.net应用程序的路径 模式窗口关闭,并刷新父窗口的方法 跨页面实现多选 用window.location.href实现刷新另个框架页面 听说是个高效的分页存储过程,可以轻松应对百万数据(转) keycode 值 DataGrid点击删除按钮弹出对话框的问题
asp.net 2.0 生成验证码
xixi8820 · 2007-12-11 · via 博客园 - xixi8820

下面的代码我已经测试成功。

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.Drawing;
 11using System.Drawing.Imaging;
 12
 13
 14
 15public partial class _Default : System.Web.UI.Page
 16{
 17    //该页面将用于生成验证码图片
 18
 19    protected void Page_Load(object sender, EventArgs e)
 20    {
 21        //调用函数将验证码生成图片
 22        this.CreateCheckCodeImage(GenerateCheckCode()); 
 23
 24    }

 25
 26    private string GenerateCheckCode()
 27    {  //产生五位的随机字符串
 28        int number;
 29        char code;
 30        string checkCode = String.Empty;
 31
 32        System.Random random = new Random();
 33
 34        for (int i = 0; i < 5; i++)
 35        {
 36            number = random.Next();
 37
 38            if (number % 2 == 0)
 39                code = (char)('0' + (char)(number % 10));
 40            else
 41                code = (char)('A' + (char)(number % 26));
 42
 43            checkCode += code.ToString();
 44        }

 45
 46        //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
 47        Session["CheckCode"= checkCode;//用于客户端校验码比较
 48
 49        return checkCode;
 50    }

 51
 52    private void CreateCheckCodeImage(string checkCode)
 53    {  //将验证码生成图片显示
 54        if (checkCode == null || checkCode.Trim() == String.Empty)
 55            return;
 56
 57        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
 58        Graphics g = Graphics.FromImage(image);
 59
 60        try
 61        {
 62            //生成随机生成器 
 63            Random random = new Random();
 64
 65            //清空图片背景色 
 66            g.Clear(Color.White);
 67
 68            //画图片的背景噪音线 
 69            for (int i = 0; i < 25; i++)
 70            {
 71                int x1 = random.Next(image.Width);
 72                int x2 = random.Next(image.Width);
 73                int y1 = random.Next(image.Height);
 74                int y2 = random.Next(image.Height);
 75
 76                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 77            }

 78
 79            Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
 80            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
 81            g.DrawString(checkCode, font, brush, 22);
 82
 83            //画图片的前景噪音点 
 84            for (int i = 0; i < 100; i++)
 85            {
 86                int x = random.Next(image.Width);
 87                int y = random.Next(image.Height);
 88
 89                image.SetPixel(x, y, Color.FromArgb(random.Next()));
 90            }

 91
 92            //画图片的边框线 
 93            g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
 94
 95            System.IO.MemoryStream ms = new System.IO.MemoryStream();
 96            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
 97            Response.ClearContent();
 98            Response.ContentType = "image/Gif";
 99            Response.BinaryWrite(ms.ToArray());
100        }

101        finally
102        {
103            g.Dispose();
104            image.Dispose();
105        }

106    }

107
108
109}

下面是测试验证码的页面:

1<div>
2        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
3        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
4        &nbsp;
5        <asp:Image ID="Image1" runat="server" ImageUrl="Default.aspx" />//该图片将用于显示验证码
6
7    </div>

 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Test : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16
17    }

18    protected void Button1_Click(object sender, EventArgs e)
19    {
20        if (this.TextBox1.Text.ToString().Trim() == Session["CheckCode"].ToString())
21        {
22            Response.Write("<script lauguage='javascript'>alert('验证成功');</script>");
23        }

24        else
25        {
26            Response.Write("<script lauguage='javascript'>alert('验证码错误,请重新输入!');</script>");
27        }

28
29    }

30}