1.新建一个aspx页面生成验证码图像

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing; public partial class NewWeb_CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
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)('A' + (char)(number % ));
checkCode += code.ToString();
}
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
Session["M2CheckCode"] = 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.Black), 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);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} }

2.在添加验证码的地方添加

<img id="ImgCheckCode" alt="验证码" src="CheckCode.aspx" onclick="reloadcode();" style="cursor:pointer;" />
 //验证码刷新
function reloadcode() {
document.getElementById("ImgCheckCode").src = "CheckCode.aspx?a=" + Math.random();
}

asp.net添加验证码的更多相关文章

  1. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  2. ASP.NET mvc 验证码 (转)

    ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...

  3. PHPCMS v9 自定义表单添加验证码验证

    1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  4. Angular企业级开发(9)-前后端分离之后添加验证码

    1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...

  5. PHPCMS v9 自定义表单添加验证码

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

  6. cas4.2.4 登添加验证码

    看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 -->      &l ...

  7. ASP.NET MVC验证码演示(Ver2)

    前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...

  8. [phpcms v9]自定义表单添加验证码验证功能

    修改  \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  9. 【转】PHPCMS v9 自定义表单添加验证码验证

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

随机推荐

  1. iOS系统app崩溃日志手动符号化

    iOS系统app崩溃日志手动符号化步骤: 1.在桌面建立一个crash文件夹,将symbolicatecrash工具..crash文件..dSYM文件放到该文件夹中 a.如何查询symbolicate ...

  2. AT&T Assembly for Linux and Mac (sys_exit)

    Exit() in C : (sys_exit.c) int main(void) { ; } Exit() in AT&T for Linux: (sys_exit.s) .section ...

  3. java基础接口练习

    1.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法voidprintCapitalLetter():在接口InterfaceB中有个方法void pri ...

  4. JavaScript核心编程(代码片段)

    var a = function () { function someSetup() { var setup = 'done'; } function actualWork() { alert('Wo ...

  5. Alcatraz的安装和使用

    一.简单说明 Alcatraz 是一款 Xcode的插件管理工具,可以用来管理XCode的 插件.模版以及颜色配置的工具. 二.如何安装 1.github地址:https://github.com/a ...

  6. 统计项目的java代码总行数

    http://www.oschina.net/code/snippet_209431_7358 wc -l $(find . -type f -name '*.java')

  7. GET请求中URL的最大长度限制总结

    由于jsonp跨域请求只能通过get请求,url长度根据浏览器及服务器的不同而有不同限制. 若要支持IE的话,最大的长度为2083字符,若是中文字符的话只有2083/9=231个字符. 若是Chrom ...

  8. HBase + Kerberos 配置示例(二)

    接上篇<HBase + Kerberos配置示例(一)>,我们继续剩下的配置工作. 环境准备 Hadoop配置 Zookeeper配置 HBase配置 Java测试程序 环境准备 安装ha ...

  9. Wireshark图解教程

    Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与很多其他网络工具一样,Wireshark也使用pcap network ...

  10. 线程互斥与析构函数中mutex的销毁

    正在实现一个线程池的pthread包装器,突然发现有人在讨论关于http://blog.csdn.net/Solstice/article/details/5238671 是一篇比较老的文章,考虑了下 ...