ASP.NET验证码制作

简单的数字与字母组合生成的那种... 简单的数字与字母组合生成的那种 展开
 我来答
秋天的秋风秋bG
2012-06-01 · TA获得超过605个赞
知道小有建树答主
回答量:1205
采纳率:0%
帮助的人:508万
展开全部
百度搜索
asp.net 验证码就出来了
image.aspx

加载事件里面:

using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

/// <summary>
/// 加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
string tmp = RndNum(6);
Session["validation"] = tmp;
HttpCookie a = new HttpCookie("ImageV ", tmp);

Response.Cookies.Add(a);
this.ValidateCode(tmp);

}
/// <summary>
/// 生成验证图片
/// </summary>
/// <param name="VNum"></param>
private void ValidateCode(string checkCode)
{

if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 2; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

private string RndNum(int VcodeNum)
{
string Vchar= "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 ";
string[] VcArray=Vchar.Split(new Char [] { ','});
string VNum= " ";
int temp=-1;

Random rand=new Random();

for(int i=1;i <VcodeNum+1;i++)
{
if(temp!=-1)
{
rand=new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}

int t=rand.Next(35);
if(temp!=-1&&temp==t)
{
return RndNum(VcodeNum);
}
temp=t;
VNum+=VcArray[t];
}
return VNum;
}

页面调用2方法:

string yanzhem =Convert.ToString(Session["validation"]);

加载事件里面: chkImageButton.ImageUrl = "image.aspx ";

1.

<img src="image.aspx" alt="验证码" height="20" width="90" onclick="this.src=this.src+'?' />

2.

<asp:ImageButton runat="server" ID="ImageUrl="image.aspx" onclick="this.src=this.src+'?' />
AiPPT
2024-09-19 广告
作为北京饼干科技有限公司的工作人员,关于AIPPT免费生成PPT的功能,我可以简要介绍如下:AIPPT是一款基于人工智能技术的PPT制作工具,它为用户提供了免费生成PPT的便捷服务。用户只需简单输入PPT的主题或内容大纲,AIPPT便能智能... 点击进入详情页
本回答由AiPPT提供
最后的小丁
2012-06-01 · TA获得超过516个赞
知道小有建树答主
回答量:490
采纳率:0%
帮助的人:142万
展开全部
<%@ 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(200, 60); Graphics g = Graphics.FromImage(b); g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60); Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel); Random r = new Random(); //合法随机显示字符列表 string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // string strLetters = "1234567890"; 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(0, 15)); } //生成干扰线条 Pen pen = new Pen(new SolidBrush(Color.Blue), 2); for (int i = 0; i < 10; 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(hc.Response.OutputStream, ImageFormat.Gif); //hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致 hc.Session["strIdentify"] = s.ToString().ToLower(); //先保存在Session中,验证与用户输入是否一致 hc.Response.End(); } /// <summary> /// 表示此类实例是否可以被多个请求共用(重用可以提高性能) /// </summary> public bool IsReusable { get { return true; } } }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Xuan0316Wang
推荐于2016-11-28 · TA获得超过1273个赞
知道小有建树答主
回答量:216
采纳率:0%
帮助的人:66.3万
展开全部
显示验证码的页(前台)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminLogin.aspx.cs" Inherits="Admin_AdminLogin" %>
<!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>Admin登陆</title>
<script type="text/javascript">
function changeimg() {
var myimg = document.getElementById("img_Check");
now = new Date(); //如果不设定此事件的话会出现只能换一次的bug
myimg.src = "AdminImage.aspx?code=" + now.getTime();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table>
<tr>
<td align="right">
验证码:<asp:TextBox ID="tb_Check" runat="server" Width="70"></asp:TextBox>
<asp:Image ID="img_Check" runat="server" ImageUrl="~/Admin/AdminImage.aspx" Visible="true"
ImageAlign="Middle" />
</td>
<td>
<a href="javascript:changeimg()">换一张 </a>
</td>
</tr>
</table>
</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;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class Admin_AdminImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;

System.Random random = new Random();

for (int i = 0; i < 5; i++)
{
number = random.Next();

if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));

checkCode += code.ToString();
}

Session["VNum"] = checkCode;

return checkCode;
}

private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;

System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
System.Drawing.Graphics g = Graphics.FromImage(image);

try
{
//生成随机生成器
Random random = new Random();

//清空图片背景色
g.Clear(Color.White);

//画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);

g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}

Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);

//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);

image.SetPixel(x, y, Color.FromArgb(random.Next()));
}

//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
释淑珍封子
2020-02-12 · TA获得超过3.7万个赞
知道大有可为答主
回答量:1.4万
采纳率:29%
帮助的人:680万
展开全部
有个随机数开投的类,r开头的,实例化时设置四位数。
把这个数存起来,
到页面引用就ok了,
中间还有封装图片什么的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式