ASP验证码代码

ASP验证码代码
ASP验证码代码

在ASP页面中生成图片验证码,需要用到System.Drawing命名空间下的很多类,首先我们需要新建一个CreateImage.aspx页面,在后台代码中定义用于生成验证码图片的方法,如下:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HTMLControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.XML.Linq;

using System.Drawing;

public partial class CreateImage : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

//产生4位验证码

string CheckCode = CreateCode(4);

//用于验证

Session["code"] = CheckCode;

CreateImages(CheckCode);

}

///

/// 产生验证码

///

///

///

public string CreateCode(int codeLength)

{

string so = "1,2,3,4,5,6,7,8,9,0,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";

string[] strArray = so.Split(',');

string code = "";

Random rand = new Random();

for (int i = 0; i < codeLength; i++)

{

//Random.Next(minValue,maxValue);

//一个大于或等于minValue 且小于maxValue 的32 位带符号整数,即:返回的值范围包括minValue 但不包括maxValue。如果

//minValue 等于maxValue,则返回minValue。

code += strArray[rand.Next(0, strArray.Length)];

}

return code;

}

///

/// 产生验证图片

///

///

public void CreateImages(string code)

{

//创建一个Bitmap新实例

Bitmap image = new Bitmap(60, 20);

Graphics g = Graphics.FromImage(image);

WebColorConverter ww = new WebColorConverter();

//清楚整个绘图面,并以制定颜色填充

g.Clear((Color)ww.ConvertFromString("#FAE264"));

Random rand = new Random();

//画图片的背景噪音线

for (int i = 0; i < 12; i++)

{

int x1 = rand.Next(image.Width);

int x2 = rand.Next(image.Height);

int y1 = rand.Next(image.Width);

int y2 = rand.Next(image.Height);

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

}

//新建字体

Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true);

g.DrawString(code, font, brush, 0, 0);

//画图片的前景噪音

for (int i = 0; i < 10; i++)

{

int x = rand.Next(image.Width);

int y = rand.Next(image.Height);

image.SetPixel(x, y, Color.White);

}

//画图片的边框线

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());

g.Dispose();

image.Dispose();

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是https://www.360docs.net/doc/263016151.html, Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

///

/// 设计器支持所需的方法- 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

以上是CreateImage.aspx文件的后台代码,完成上述方法后,在页面里调用方法,只需要添加,即可显示生成的验证码图片,每次刷新都会随机产生不同的验证码,如下:

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

CheckImage

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

public class Rand

{

public string rand;

Random ran = new Random(https://www.360docs.net/doc/263016151.html,lisecond);//加个随机数种子

public Rand()

{

for (int i = 0; i < 4; i++)

{

this.rand += ran.Next(0, 16).ToString("x1");

}

HttpContext.Current.Session.Add("code",this.rand);

}

}

public partial class image : System.Web.UI.Page

{

Random ran = new Random(https://www.360docs.net/doc/263016151.html,lisecond);//加个随机数种子public int x,y,p,q;

public int r, g, b;

protected void draw()

{

Rand R = new Rand();

Session["image"] = R.rand;

Bitmap image = new Bitmap(100, 50);//创建画布;

Graphics g = Graphics.FromImage(image);//创建画图对象;

g.Clear(Color.Black);

SolidBrush blackbrush = new SolidBrush(Color.White);

Font font = new Font("Verdana", 14);

for (int i = 0; i < 100; i++)

{

this.x = ran.Next(0, 100);

this.y = ran.Next(0, 50);

this.p = ran.Next(0, 100);

this.q = ran.Next(0, 50);

this.r = ran.Next(0, 256);

this.g = ran.Next(0, 256);

this.b = ran.Next(0, 256);

Pen pen = new Pen(Color.FromArgb(this.r, this.g, this.b));

g.DrawLine(pen, this.x, this.y, this.x, this.y + 1);

//g.DrawLine(pen, this.x, this.y, this.x + this.p, this.y + this.q);

}

g.DrawString(R.rand, font, blackbrush, 20, 10);

Response.ContentType = "image/jpeg";

image.Save(Response.OutputStream,

System.Drawing.Imaging.ImageFormat.Jpeg);//输出图片;

blackbrush.Dispose();//释放画笔;

g.Dispose();//释放绘图对象;

image.Dispose();//释放图形对象;

}

protected void Page_Load(object sender, EventArgs e) {

draw();

}

}

javascript常用代码大全

Javascript常用代码大全 //打开模式对话框 //返回模式对话框的值 function okbtn_onclick() { var commstr=''; window.returnValue=commstr; window.close() ; } okbtn_onclick() //全屏幕打开IE 窗口 var winWidth=screen.availWidth ; var winHeight=screen.availHeight-20; window.open("main.aspx","surveyWindow","toolbar=no,wid th="+ winWidth +",height="+ winHeight +",top=0,left=0,scrollbars=yes,resizable=yes,center:yes,statu sbars=yes"); //脚本中中使用xml function initialize() { var xmlDoc var xslDoc xmlDoc = new ActiveXObject('Microsoft.XMLDOM') xmlDoc.async = false; xslDoc = new ActiveXObject('Microsoft.XMLDOM') xslDoc.async = false; xmlDoc.load("tree.xml") xslDoc.load("tree.xsl") folderTree.innerHTML = xmlDoc.documentElement.transformNode(xslDoc) } 一、验证类 1、数字验证内 1.1 整数 1.2 大于0的整数(用于传来的ID的验证) 1.3 负整数的验证 1.4 整数不能大于iMax 1.5 整数不能小于iMin 2、时间类 2.1 短时间,形如(13:04:06) 2.2 短日期,形如(2003-12-05) 2.3 长时间,形如(2003-12-05 13:04:06) 2.4 只有年和月。形如(2003-05,或者2003-5) 2.5 只有小时和分钟,形如(12:03) 3、表单类 3.1 所有的表单的值都不能为空 3.2 多行文本框的值不能为空。 3.3 多行文本框的值不能超过sMaxStrleng 3.4 多行文本框的值不能少于sMixStrleng 3.5 判断单选框是否选择。 3.6 判断复选框是否选择. 3.7 复选框的全选,多选,全不选,反选 3.8 文件上传过程中判断文件类型 4、字符类 4.1 判断字符全部由a-Z或者是A-Z的字字母组成 4.2 判断字符由字母和数字组成。 4.3 判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母 4.4 字符串替换函数.Replace(); 5、浏览器类 5.1 判断浏览器的类型 5.2 判断ie的版本 5.3 判断客户端的分辨率 6、结合类 6.1 email的判断。 6.2 手机号码的验证 6.3 身份证的验证 二、功能类 1、时间与相关控件类 1.1 日历 1.2 时间控件 1.3 万年历 1.4 显示动态显示时钟效果(文本,如OA中时间) 1.5 显示动态显示时钟效果(图像,像手表) 2、表单类 2.1 自动生成表单 2.2 动态添加,修改,删除下拉框中的元素 2.3 可以输入内容的下拉框 2.4 多行文本框中只能输入iMax文字。如果多输入了,自动减少到iMax个文字(多用于短信发送) 3、打印类 3.1 打印控件 4、事件类 4.1 屏蔽右键 4.2 屏蔽所有功能键 4.3 --> 和<-- F5 F11,F9,F1 4.4 屏蔽组合键ctrl+N 5、网页设计类 5.1 连续滚动的文字,图片

自动生成验证码图片的工具类,收藏起来备用。

[原]自动生成验证码图片的工具类,收藏起来备用。用法:直接调用其静态方法即可。 Map map = ImageUtil.getImage();//获取图片,将图片转换成InputStream流 String key = map.keySet().iterator().next();//获取图片上的字符 BufferedImage image = map.get(key);//获取BufferedImage,赋值给imageStream其中,String为验证码的值,BufferedImage为验证码的图片。 该工具类的代码如下:ImageUtil.java package action; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import java.util.Random; public final class ImageUtil { private static final String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "东", "南", "西", "北", "中", "发", "白" }; private static final int SIZE = 5;//验证码长度 private static final int LINES = 20;//干扰线条数 private static final int WIDTH = 200;//图片宽度 private static final int HEIGHT = 100;//图片高度 private static final int FONT_SIZE = 60;//字体高度 public static Map getImage() { StringBuffer sb = new StringBuffer(); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics graphic = image.getGraphics(); graphic.setColor(Color.LIGHT_GRAY); graphic.fillRect(0, 0, WIDTH, HEIGHT); Random ran = new Random(); //画随机字符 for(int i=1;i<=SIZE;i++){ int r = ran.nextInt(chars.length); graphic.setColor(getRandomColor()); graphic.setFont(new Font(null,Font.BOLD+Font.ITALIC,FONT_SIZE));

C#生成随机验证码例子

C#生成随机验证码例子: 前端: 1 2 3验证码: 4 5 6 给验证码图片绑定单击事件: $("#valiateCode").click(function () { $("#imgCode").attr("src",$("#imgCode").attr("src")+1); }); 后台生成验证码图片代码: ValidateCode.ashx 1 <%@ WebHandler Language="C#" Class="ValidateCode" %> 2 3using System; 4using System.Web; 5using System.Drawing; 6using System.Web.SessionState; 7 8public class ValidateCode : IHttpHandler, IRequiresSessionState 9 { 10 HttpContext context; 11public void ProcessRequest (HttpContext context1) { 12this.context = context1; 13 CreateCheckCodeImage(GenerateCheckCode()); 14 }

JSP生成验证码_源代码

jsp验证码实现源代码 (2011-10-16 11:11:40) 验证码原理:①首先随机生成数字或字母;②把生成的数字或字母保存在Session这次会话中;③最后,在登陆页面上把接收用户输入的验证码与生成的验证码相匹配,成功返回TRUE,失败则返回FALSE; 演示程序包括三个文件: 1.login.jsp:登录页面 2.code.jsp:生成验证码图片页面 3.check.jsp:验证结果 code.jsp <%@ page contentType="image/jpeg" import="java.awt.*, java.awt.image.*,java.util.*,javax.imageio.*" %> <% // 在内存中创建图象 int width=60, height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 设定背景色 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, width, height); //画边框 g.setColor(Color.black); g.drawRect(0,0,width-1,height-1); // 随机产生的认证码(4位数字) String rand =""+ (Math.random()*10000); rand = rand.substring(0,rand.indexOf(".")); switch(rand.length()) { case 1: rand = "000"+rand; break; case 2: rand = "00"+rand; break; case 3: rand = "0"+rand; break; default: rand = rand.substring(0,4); break; }

验证码自动_获取平台

验证码自动_获取平台 目录 1、验证码是什么 2、短信验证证码接收平台干什么用的 3、短信验证码接收平台的特点 4、短信验证码接收平台如何下载 5、短信验证码接收平台的功能 1、验证码是什么 所谓的验证码又称校验码、附加码,常祖政和卢晓倩在《计算机安全与维护》期刊中共同撰写的《Java Web中随机汉字扭曲验证码的实现》一文中提到,验证码的英文是CAPTCHA,就是将一串随机产生的数字或符号,生成一幅图片,图片里加上一些干扰象素,由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。 2、短信验证证码自动_获取平台干什么用的 现在越来越多的人都需要在网上注册账户,而注册账号又必须手机验证,但是很多人发现有些账户注册完几天后就开始莫名的接到多个陌生电话和短信,大部分都是以推销为目的,所以越

来越多的人都希望既能顺利完成注册又不会给自己带来不必要的麻烦,所以有人就开始解决这样的需求。 他们会联系卡商,通过卡商提供的号码,为注册账号验证提供号码,并把收到第三方的验证码内容显示在平台上,然后把验证码上的内容填入注册页面,就顺利完成注册, 这样陌生电话就不会打到你自己使用的电话上了。可以搜索“=y码=”得到详细的了解。 3、短信验证码自动_获取平台的特点 如今的互联网隐私被人们看的越来越重要,但是大多数网站在注册账户时、或要验证某些东西时强制用户输入自己的手机号接收验证码进行验证。这样对个人隐私造成很大的威胁。验证码接收平台就是用来解决这一问题的系统平台。用户可以利用验证码接收平台的手机号码来代接短信验证码进行验证,保护自己的隐私不受侵犯。每种验证码都有某种固定的规律。如发送号码,短信开头,结尾,关键字。验证码接收平台的特点: 1、全自动收发手机验证码,速度快 2、可以获取任意合法第三方网站手机短信验证码的系统平台。 3、解决注册多个帐号,手机认证的问题。 4、可获取的号码量多、验证码项目齐全,并拥有即时响应的服务。 5、集各类网站用户账号注册、绑定、验证、解封等手机验证码的收发功能于一身。

c#生成验证码的方法

新建一个WaterMark.ASHX文件,将如下代码Copy进去 <%@ WebHandler Language="C#" Class="WaterMark" %> using System; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; using System.Web.SessionState; public class WaterMark : IHttpHandler, IRequiresSessionState // 要使用session必须实现该接口,记得要导入System.Web.SessionState命名空间 { public void ProcessRequest(HttpContext context) { string checkCode = GenCode(5); // 产生5位随机字符 context.Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证 System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); // 画图片的背景噪音线 int i; for (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.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);

JavaScript 经典代码大全

代码一 1. oncontextmenu="window.event.returnvalue=false" 将彻底屏蔽鼠标右键

no
可用于Table 2. 取消选取、防止复制 3. onpaste="return false" 不准粘贴 4. oncopy="return false;" oncut="return false;" 防止复制 5. IE地址栏前换成自己的图标 6. 可以在收藏夹中显示出你的图标 7. 关闭输入法 8. 永远都会带着框架 9. 防止被人frame 10. 网页将不能被另存为 11. 12.删除时确认 删除 13. 取得控件的绝对位置 //javascript //VBScript