Asp.net mvc生成验证码
1.生成验证码类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Web; namespace SimpleNews.FrontEnd
{
public class ToolController : MyControllerBase
{
/// <summary>
/// 生成验证码字符串
/// </summary>
/// <param name="codeLen">验证码字符长度</param>
/// <returns>返回验证码字符串</returns>
private static string MakeCode(int codeLen)
{
if (codeLen < )
{
return string.Empty;
}
int number;
StringBuilder sbCheckCode = new StringBuilder();
Random random = new Random(); for (int index = ; index < codeLen; index++)
{
number = random.Next(); if (number % == )
{
sbCheckCode.Append((char)('' + (char)(number % ))); //生成数字
}
else
{
sbCheckCode.Append((char)('A' + (char)(number % ))); //生成字母
}
}
return sbCheckCode.ToString();
} ///<summary>
/// 获取验证码图片流
/// </summary>
/// <param name="checkCode">验证码字符串</param>
/// <returns>返回验证码图片流</returns>
public static MemoryStream CreateCodeImg(string checkCode)
{
if (string.IsNullOrEmpty(checkCode))
{
return null;
}
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), );
Graphics graphic = Graphics.FromImage(image);
try
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = , y1 = , x2 = , y2 = ;
for (int index = ; index < ; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height); graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", , (FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(checkCode, font, brush, , ); int x = ;
int y = ; //画图片的前景噪音点
for (int i = ; i < ; i++)
{
x = random.Next(image.Width);
y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
graphic.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
//将图片验证码保存为流Stream返回
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
graphic.Dispose();
image.Dispose();
}
} /// <summary>
/// 获取验证码
/// </summary>
/// <returns></returns>
public ActionResult GetValidateCode()
{
string code = MakeCode();
Session["ValidateCode"] = code;
MemoryStream ms = CreateCodeImg(code);
return File(ms.ToArray(), @"image/jpeg");
} }
}
2.前端Html页面
<span>验证码:<input name="ValidateCode" type="text" value="" class="input2" id="txtValidateCode" size="" maxlength="" >
<img src="../Tool/GetValidateCode" style="cursor: pointer;" name="checkcode" border=""
id="valiCode" alt="验证码" class="Ysm" />
</span>
$(function () {
$("#valiCode").bind("click", function () {
this.src = "../Tool/GetValidateCode?time=" + (new Date()).getTime();
});
});
3.后台验证输入的验证码
string validateCode = (string)Request.Form["ValidateCode"]??"";
if (string.IsNullOrEmpty(validateCode))
{
ModelState.AddModelError("validateCodeError","验证码错误!");
return View();
}
if (Session["ValidateCode"]!=null&&Session["ValidateCode"].ToString().ToLower() != validateCode.ToLower())
{
ModelState.AddModelError("validateCodeError", "验证码错误!");
return View();
}
Asp.net mvc生成验证码的更多相关文章
- (一)【转】asp.net mvc生成验证码
网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中. ...
- ASP.NET MVC 生成验证码
using System.Web.Mvc; using System.Drawing; using System; using System.Drawing.Imaging; using Models ...
- ASP.Net MVC 生成安全验证码
---------html <td>验证码:</td> <td> <img src="/Logi ...
- ASP.NET MVC生成安全验证码
html部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- ASP.NET MVC5 生成验证码
1 ValidateCode.cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.D ...
- C#工具:ASP.NET MVC生成图片验证码
1.复制下列代码,拷贝到控制器中. #region 生成验证码图片 // [OutputCache(Location = OutputCacheLocation.None, Duration = 0, ...
- ASP.NET MVC 生成EML文件
需求: 点发送邮件按钮的时候, 自动在客户端电脑打开默认邮件的窗口,并且把内容和附件都附加上去. 解决方案: 尝试使用过Microsoft.Office.Interop.Outlook 和 MPAI. ...
- asp.net mvc 生成条形码
using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...
- Asp.net MVC 生成zip并下载
前面有生成Excel或Word的示例,所以就不再重新写了. 这里只提供将指定文件以ZIP的方式下载. 创建一个 Zip工具类 public class ZIPCompressUtil { public ...
随机推荐
- 为什么在注册和注销的时候intent要改成隐式调用
显式意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件. 隐式意图: ...
- TEST设置
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
- Archlinux 添加无线网络
From: https://wiki.archlinux.org/index.php/Beginners%27_Guide_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%8 ...
- 纸上谈兵:表(list)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 表 表(list)是常见的数据结构.从数学上来说,表是一个有序的元素集合.在C语言 ...
- 通过案例对 spark streaming 透彻理解三板斧之三:spark streaming运行机制与架构
本期内容: 1. Spark Streaming Job架构与运行机制 2. Spark Streaming 容错架构与运行机制 事实上时间是不存在的,是由人的感官系统感觉时间的存在而已,是一种虚幻的 ...
- idea 到myeclipse
在上一篇博客使用maven进行开发过程管理之准备篇中提到了maven的基本概念.IT男罗书全觉得概念我是懂了,但是那些东西似乎离我很远啊.先开发再说吧, 于是IT男罗书全就在svn上取了源代码,并开始 ...
- 32、shiro框架入门3.授权
一. 授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission ...
- display:inline-block
/* inline为行内元素不自动换行,不占用文档流,也就是说你在这个后面写一个元素这个元素会并排显示.block为块元素,单独占一行文档,并可以给这个块元素添加宽高背景颜色.而inline-bloc ...
- 进阶篇:以IL为剑,直指async/await
接上篇:30分钟?不需要,轻松读懂IL,这篇主要从IL入手来理解async/await的工作原理. 先简单介绍下async/await,这是.net 4.5引入的语法糖,配合Task使用可以非常优雅的 ...