ASP.NET 实现验证码以及刷新验证码
实现代码
/// <summary>
/// 生成验证码图片,保存session名称VerificationCode
/// </summary>
public static void CreateVerificationCode()
{
int number;
string checkCode = string.Empty; //随机数种子
Random randoms = new Random(); for (int i = ; i < ; i++) //校验码长度为4
{
//随机的整数
number = randoms.Next(); //字符从0-9,A-Z中随机产生,对应的ASCII码分别为
//48-57,65-90
number = number % ;
if (number < )
{
number += ;
}
else
{
number += ;
}
checkCode += ((char)number).ToString();
} //在session中保存校验码
System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode; //若校验码为空,则直接返回
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
//根据校验码的长度确定输出图片的长度
System.Drawing.Bitmap image = new System.Drawing.Bitmap(, );//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
//创建Graphics对象
Graphics g = Graphics.FromImage(image);
try
{
//生成随机数种子
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线 10条
//---------------------------------------------------
for (int i = ; i < ; i++)
{
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
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);
}
//---------------------------------------------------
//Brush b = Brushes.Silver;
//g.FillRectangle(b, 0, 0, image.Width, image.Height);
//---------------------以上两种任选其一------------------------------
//输出图片中校验码的字体: 12号Arial,粗斜体
Font font = new Font("Arial", , (FontStyle.Bold | FontStyle.Italic)); //线性渐变画刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
g.DrawString(checkCode, font, brush, , ); //画图片的前景噪音点 50个
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.Peru), , , image.Width - , image.Height - ); //创建内存流用于输出图片
using (MemoryStream ms = new MemoryStream())
{
//图片格式指定为png
image.Save(ms, ImageFormat.Jpeg);
//清除缓冲区流中的所有输出
System.Web.HttpContext.Current.Response.ClearContent();
//输出流的HTTP MIME类型设置为"image/Png"
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
//输出图片的二进制流
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
}
finally
{
//释放Bitmap对象和Graphics对象
g.Dispose();
image.Dispose();
}
}
Create Verification Code
创建一个aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %> <%Help.CreateVerificationCode(); %>
Create WebFrom
添加HTML代码,引用
<div class="positionR">
<label>验证码:</label>
<span class="style1"> *</span>
<input type="text" class="yanZm" runat="Server" reg="^.+$" id="txtAuthCode" tip="请输入验证码!" />
<img class="yanZm_img" src="AuthCode.aspx" alt="" id="imgAuthCode" />
</div>
HTML Code
如何实现刷新?
<script type="text/javascript">
$("#imgAuthCode").click(function () {
$(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime());
});
</script>
Refresh - JS Code
效果图

注明:内容来源不可考 = = 验证码图片生成非原创,如果谁知道原作者请告诉我.
ASP.NET 实现验证码以及刷新验证码的更多相关文章
- ASP.NET MVC3实现无刷新验证码
在MVC中进行留言,评论等功能时,不可避免会用到表单提交时的验证码问题,有时,我们的作法是,当表单被提交后,在controller里去判断验证码的正确与否,但我认为这种用户体验是很差的,今天正好有后时 ...
- yourphp点击刷新验证码
加入css <script type="text/javascript" src="./Public/Js/my.js"></script&g ...
- J2EE如何生成验证码图片和点击刷新验证码
验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...
- ASP.NET 如何做出简单的验证码
如果说要做验证码,那不得不提的就是GDI+绘图了.我们都知道验证码是以图片形式展示的,而且是动态生成的,这样就需要我们去画出它. 科普一下,什么是GDI+? GDI+是图形设备接口(GDI)的高级版本 ...
- 解决Yii2中刷新网页时验证码不刷新的问题
解决Yii2中刷新网页时验证码不刷新的问题 [ 2.0 版本 ] ljfrocky 2015-05-30 19:39:00 1304次浏览 5条评论 10110 在Yii2框架中,如果在表单中使用 ...
- C#之asp.net 及MVC 生成动态验证码:
C#之asp.net 及MVC 生成动态验证码: 1.生成验证码字符串 // 随机生成指定长度的验证码字符串private string RandomCode(int length) { string ...
- 用户登录ajax局部刷新验证码
用户登录的时候,登录页面附带验证码图片,用户需要输入正确的验证码才可以登录,验证码实现局部刷新操作. 效果如图: 代码如下: #生成验证码及图片的函数 newcode.py import rando ...
- S2SH框架中的无刷新验证码功能实现
暑假期间在实验室做使用S2SH框架的项目,其中登录和注册需要验证码,实现了一个没有实现刷新验证码功能的简单版本,代码如下: 1 package com.sem.action; 2 3 import j ...
- [oldboy-django][2深入django]点击刷新验证码
# 点击更新验证码,只要重新在发送一个请求即可 <img src="/check_code/" onclick="updateCode(this);" w ...
随机推荐
- EL表达式、 jstl标签
https://www.cnblogs.com/zhaotiancheng/p/6391894.html https://blog.csdn.net/zdwzzu2006/article/detail ...
- java并发编程:线程安全管理类--原子操作类--AtomicReferenceArray<E>
1.类 AtomicReferenceArray<E> public class AtomicReferenceArray<E>extends Objectimplements ...
- forget word out a~2
1● an 不,非,无 2● amphi 两个,两种 3● ad 做,加强:
- vs2015 系统找不到指定的文件(异常来自HRESULT:0x80070002)问题的解决方法
vs2015 创建mvc项目时,弹出错误信息内容(系统找不到指定的文件(异常来自HRESULT:0x80070002)) 弹出窗体如下图所示: 导致整个原因是:未安装NuGet包 解决方法: 1)打开 ...
- substr和substring,slice和splice的区别,js字符串截取和数组截取
本文参考了文章:https://blog.csdn.net/kenberkeley/article/details/50983734 博主已经总结得很好了,看完之后也能明白,不过还是觉得要自己动手敲一 ...
- css div居中
div中没有text-align和vehicle-align这两个属性,所在在css中设置这两个值没有作用 div 水平居中 设置左右边距有auto div{ margin: 0 auto; } 垂直 ...
- json 与字符串相互转换,
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- 通过url获取参数信息
运行结果如下: <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <ti ...
- 北邮新生排位赛2解题报告d-e
<> 427. 学姐逗学弟 时间限制 3000 ms 内存限制 131072 KB 题目描述 学弟们来了之后,学姐每天都非常高兴的和学弟一起玩耍.这一天,学姐想出了这样一个游戏,她画了一棵 ...
- QT环境下实现UI界面的“拼图游戏”
main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *ar ...