Asp.net验证码-顶誉参考源代码

<%@ 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 id="form1" runat="server">
<div>
<!--验证码 -->
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Handler.ashx" οnclick="this.src=this.src+'?'"/>
<hr />
<!--?表示传递的是空参数引起页面的刷新-->
<img alt="" src="Handler.ashx" οnclick="this.src=this.src+'?'"/>
<hr />
<asp:Button ID="Button1" runat="server" Text="验证" οnclick="Button1_Click"
/>
</div>
</form>
</body>
</html>

--------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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 (Session["myID"].ToString() == this.TextBox1.Text)
{
Response.Write("OK!");
}
else
{
Response.Write("No!");
}

}
}

----------------------------------------------------------------------------------------------------------------

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

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

public class Handler : IHttpHandler,IRequiresSessionState
{
private const int totalLength = 5; //声明验证码总的长度

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(200, 60);
//准备画布
Graphics g = Graphics.FromImage(b);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);

//随机产生字串
Random R = new Random();
//原始字串
string letters = "qazwsxedcrfvtgbyhnujmikolp";
StringBuilder s = new StringBuilder();
for (int i = 0; i < totalLength; i++)
{
s.Append(letters.Substring(R.Next(0, letters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.White), i * 38, R.Next(0, 15));
}

//干扰线
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
for (int i = 0; i < 5; i++)
{
g.DrawLine(pen, new Point(R.Next(0, 199), R.Next(0, 59)), new Point(R.Next(0, 199), R.Next(0, 59)));
}
b.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["myID"] = s.ToString();
context.Response.End();
g.Dispose();
b.Dispose();
}

public bool IsReusable
{
get
{
return false;
}
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值