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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 残叶

Enum视为位域 Asp.net 2.0生命周期 - 残叶 ASP.NET 2.0 的 App_Offline.htm - 残叶 联合使用抽象类和接口 - 残叶 C#中抽象类和接口的区别 C#静态构造函数 封装K2.net 2003中的K2ROM Asp.net开发小技巧 跨页面提交数据的方法 页面的重定向 - 残叶 (转)Asp.net 中 Get和Post 的用法 (转)关于ASP.net运行流程 O/R Mapping (Object-Relation Mapping,对象关系映射) - 残叶 网络主机名与IP C#发送Email方法总结 @@IDENTITY与SCOPE_IDENTITY() .NET和SQL Server中“空值”辨析 - 残叶 Transactions - 残叶 回家了 - 残叶
ASP.NET 2.0 HttpHandler实现生成图片验证码(转)
残叶 · 2007-09-26 · via 博客园 - 残叶

 1. 处理程序文件 ValidateImageHandler.ashx代码如下

<%@ WebHandler Language="C#" Class="ValidateImageHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

/// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>

public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
{
    
int intLength = 5;               //长度
    string strIdentify = "Identify"//随机字串存储键值,以便存储到Session中
    public ValidateImageHandler()
    
{        
    }


    
/// <summary>
    
///  生成验证图片核心代码
    
/// </summary>
    
/// <param name="hc"></param>

    public void ProcessRequest(HttpContext hc)
    
{
        
//设置输出流图片格式
        hc.Response.ContentType = "image/gif";
        
        Bitmap b 
= new Bitmap(20060);
        Graphics g 
= Graphics.FromImage(b);
        g.FillRectangle(
new SolidBrush(Color.YellowGreen), 0020060);
        Font font 
= new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r 
= new Random();

        
//合法随机显示字符列表
        string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder s 
= new StringBuilder();
        
        
//将随机生成的字符串绘制到图片上
        for (int i = 0; i < intLength; i++)
        
{
            s.Append(strLetters.Substring(r.Next(
0, strLetters.Length - 1), 1));
            g.DrawString(s[s.Length 
- 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(015));
        }


        
//生成干扰线条
        Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
        
for (int i = 0; i < 10; i++)
        
{
            g.DrawLine(pen, 
new Point(r.Next(0199), r.Next(059)), new Point(r.Next(0199), r.Next(059)));
        }

        b.Save(hc.Response.OutputStream, ImageFormat.Gif);
        hc.Session[strIdentify] 
= s.ToString(); //先保存在Session中,验证与用户输入是否一致
        hc.Response.End();
   
    }

    
    
/// <summary>
    
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
    
/// </summary>

    public bool IsReusable
    
{
        
get
        
{
            
return true;
        }

    }

}


2.前台代码

<%@ 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>ImageValidateCode_HttpHandler</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
&nbsp;<br />
            
<br />
            
<asp:Login ID="Login1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4"
                BorderStyle
="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"
                ForeColor
="#333333" OnAuthenticate="Login1_Authenticate">
                
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
                
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
                
<TextBoxStyle Font-Size="0.8em" />
                
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px"
                    Font-Names
="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
                
<LayoutTemplate>
                    
<table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse">
                        
<tr>
                            
<td style="width: 292px">
                                
<table border="0" cellpadding="0">
                                    
<tr>
                                        
<td align="center" colspan="2" style="font-weight: bold; font-size: 0.9em; color: white;
                                            background-color: #507cd1"
>
                                            登录
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="left" style="width: 84px; height: 31px;">
                                            
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label></td>
                                        
<td style="height: 31px; width: 215px;">
                                            
<asp:TextBox ID="UserName" runat="server" Font-Size="0.8em" Width="113px"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                ErrorMessage
="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="left" style="width: 84px">
                                            
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label></td>
                                        
<td style="width: 215px">
                                            
<asp:TextBox ID="Password" runat="server" Font-Size="0.8em" TextMode="Password"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                                ErrorMessage
="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td style="width: 84px; height: 4px;" align="left">
                                            验证码:
</td>
                                        
<td valign="middle" style="height: 31px; width: 215px;" align="left">
                                            
<asp:TextBox ID="TextBox1" runat="server" Font-Size="0.8em" TextMode="Password"></asp:TextBox>&nbsp;
                                            
<img width="100px" height="25px" src="ValidateImageHandler.ashx" /></td>
                                    
</tr>
                                    
<tr>
                                        
<td align="left" colspan="2" style="color: red">
                                            
<asp:CheckBox ID="RememberMe" runat="server" Text="下次记住我。" />&nbsp;</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right" colspan="2">
                                            
<asp:Button ID="LoginButton" runat="server" BackColor="White" BorderColor="#507CD1"
                                                BorderStyle
="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
                                                Font-Size
="0.8em" ForeColor="#284E98" Text="登录" ValidationGroup="Login1" />
                                        
</td>
                                    
</tr>
                                
</table>
                            
</td>
                        
</tr>
                    
</table>
                
</LayoutTemplate>
            
</asp:Login>
        
</div>
    
</form>
</body>
</html>