C#生成随机验证吗例子
C#生成随机验证码例子:
前端:
<tr>
<td width="24%" height="" align="center" valign="top">
验证码:</td>
<td valign="top" width="37%" align="left">
<input type="text" name="txtCode" id="txtvalidateCode"/><a href="javascript:void(0)" id="valiateCode"><img src="/ashx/ValidateCode.ashx?id=1" id="imgCode" /></a><span style="font-size:14px;color:red " id="userCodeMsg"></span></td>
</tr>
给验证码图片绑定单击事件:
$("#valiateCode").click(function () {
$("#imgCode").attr("src",$("#imgCode").attr("src")+1);
});
后台生成验证码图片代码:
ValidateCode.ashx
<%@ WebHandler Language="C#" Class="ValidateCode" %> using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState; public class ValidateCode : IHttpHandler, IRequiresSessionState
{
HttpContext context;
public void ProcessRequest (HttpContext context1) {
this.context = context1;
CreateCheckCodeImage(GenerateCheckCode());
} private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = ; i < ; i++)
{
number = random.Next(); if (number % == )
code = (char)('' + (char)(number % ));
else
code = (char)('' + (char)(number % ));
//code = (char)('A' + (char)(number % 26)); checkCode += code.ToString();
} //添加Session值
context.Session.Add("vCode", 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)), );
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random(); //清空图片背景色
g.Clear(Color.White); //画图片的背景噪音线
for (int i = ; i < ; 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", , (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, , ); //画图片的前景噪音点
for (int i = ; i < ; 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), , , image.Width - , image.Height - ); System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} public bool IsReusable {
get {
return false;
}
} }
验证码效果:
C#生成随机验证吗例子的更多相关文章
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- 如何用jsp页面生成随机的验证数字码
checkNum.jsp <%@ page language="java" import="java.util.*,java.sql.*" pageEnc ...
- C#生成随机验证码
使用YZMHelper帮助类即可 using System; using System.Web; using System.Drawing; using System.Security.Cryptog ...
- C#产生不重复的随机数并生成随机文件名
本文转自:http://blog.ciznx.com/post/csharprandomnumberandrandomfilename.aspx 在项目中会遇到需要批量生成文件的时候,比如 asp.n ...
- struts---JSP界面验证码生成与验证
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- php生成随机产生六位数密码的代码
php生成随机产生六位数密码的代码,供大家学习参考.本文转自:http://www.jbxue.com/article/6199.html php生成随机产生六位数密码的代码,供大家学习参考. 复制代 ...
- .net生成随机验证码图片
/// <summary> /// 自定义图片验证码函数 /// 该函数将生成一个图片验证码,并将生成的code存放于Session["VerifyCode"]变量内. ...
- PHP 用session与gd库实现简单验证码生成与验证的类
验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的.目前大大小小的网站都有验证码.今天自己实现了一个简单的验证码类.说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下.当然,因为 ...
- 要求根据RandomStr.java:使用类型转换生成六位验证字符串,示例程序每次运 行时,都会生成不同的字符串。
1.程序设计思想验证码 ①定义一个字符串变量来保存随机生成的. ②利用循环产生六位随机数,在产生每一位时将其转换为char类型并写在字符串后面. ③利用对话框显示生成的验证码,并提示用户输入验证码. ...
随机推荐
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
今天做网页到了测试和数据库交互的地方,其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和uni ...
- [转]Android之Context和Activity互相转换
1.context转换为activity Activity activity = (Activity) context; 2.从activity得到context 在activity的方法中用cont ...
- jQuery 图片随滚动条滚动加载
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU2952:Counting Sheep(DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- Light OJ 1006 - Hex-a-bonacci
题目链接:http://acm.hust.edu.cn/vjudge/contest/121396#problem/G http://lightoj.com/volume_showproblem.ph ...
- P3414 SAC#1 - 组合数
题目背景 本题由世界上最蒟蒻最辣鸡最撒比的SOL提供. 寂月城网站是完美信息教室的官网.地址:http://191.101.11.174/mgzd . 题目描述 辣鸡蒟蒻SOL是一个傻逼,他居然觉得数 ...
- git rebase 使用
git rebase 不会取回代码 要用git fetch先取回, git rebase 是合并代码. (1)首先用git fetch返回服务器上的代码 (2)首先用git rebase origin ...
- PHP开源CRM-推荐几个
http://www.xinyou88.com/about/xcrm.html 因为医院要同步用户到诊统计的信息.是拿着表单来回送.途中大概有20分钟左右.有些机器和互联网可以搞定的事情.人力来做.在 ...
- Entity Framework 学习中级篇1—EF支持复杂类型的实现
本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作. 通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Las ...
- Apache添加mime类型
今天同事给我提了一个bug,说IE浏览器无法下载网站上的固件版本文件(xxxx.img),点击下载后显示一堆二进制乱码,因为我们公司的固件版本文件是以.img结尾的,所以对应的content-type ...