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 ...
随机推荐
- harbor私有镜像仓库的搭建与使用与主从复制
harbor私有镜像仓库,私有仓库有两种,一种是harbor,一种是小型的私有仓库,harbor有两种模式,一种是主 从,一种是高可用仓库,项目需求,需要两台服务器,都有docker.ldap权限统一 ...
- 51nod算法马拉松28-c
题解: 按照每一个要求,分类讨论,讨论压下去了多少 代码: #include<bits/stdc++.h> using namespace std; ,N=; int n,A,B,C,an ...
- vue 表格导出excel
首先要install两个依赖, 1 npm install -S file-saver xlsx 2 npm install -D script-loader 在项目src目录下新建一个文件夹ven ...
- 2.3 C++类的信息隐藏机制 -- 封装
参考:http://www.weixueyuan.net/view/6335.html 总结: private.protected和public 限制类中声明的变量和函数在外部的访问权限. 声明为p ...
- CriticalSection 临界区
// 临界区.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include<windows.h>#include<iost ...
- 2.spring 学习
1.spring简单工程搭建 http://www.cnblogs.com/yun965861480/p/6278193.html 2.spring数据源 http://www.cnblogs.com ...
- sping整合redis,以及做mybatis的第三方缓存
一.spring整合redis Redis作为一个时下非常流行的NOSQL语言,不学一下有点过意不去. 背景:学习Redis用到的框架是maven+spring+mybatis(框架如何搭建这边就不叙 ...
- UI基础:UILabel.UIFont 分类: iOS学习-UI 2015-07-01 19:38 107人阅读 评论(0) 收藏
UILabel:标签 继承自UIView ,在UIView基础上扩充了显示文本的功能.(文本框) UILabel的使用步骤 1.创建控件 UILabel *aLabel=[[UILabel alloc ...
- eclipse运行报java.lang.OutOfMemoryError: PermGen space解决方法
一.在window下eclipse里面Server挂的是tomcat6,一开始还是以为,tomcat配置的问题,后面发现,配置了tomcat里面的catalina.bat文件,加入 set JAVA_ ...
- QLabel 内容长度超过控件本身长度时超出的部分用省略号
void JunEditableLabel::setText(const QString &string) { QFont font("Tahoma"); font.set ...