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 ...
随机推荐
- 读CopyOnWriteArrayList有感
除了加锁外,其实还有一种方式可以防止并发修改异常,这就是将读写分离技术(不是数据库上的). 先回顾一下一个常识: 1.JAVA中“=”操作只是将引用和某个对象关联,假如同时有一个线程将引用指向另外一个 ...
- Android Gradle 技巧之一: Build Variant 相关
Build Variant android gradle 插件,允许对最终的包以多个维度进行组合. BuildVariant = ProductFlavor x BuildType 两个维度 最常见的 ...
- Farey Sequence
Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rationa ...
- [zhang] ViewController的生命周期分析和使用
iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面是我的一些经验. 一.结构 按结构可以对iOS的所有ViewController分成两类:1.主要用于展示内容的 ...
- HDOJ2222 Keywords Search-AC自动机
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- 2016-07-07: 重新编译时vc90.pdb不是创建此预编译头时使用的pdb文件
使用VS2008在一个解决方案中包含多个项目时,当设置多个项目的中间目录为同一个目录时,在增量编译时出现"重新编译时vc90.pdb不是创建此预编译头时使用的pdb文件,请重新创建预编译头问 ...
- HtmlAgilityPack下载开启压缩的页面乱码
当一个被采集的网页是开启压缩了的话,如果使用HtmlAgilityPack 的HtmlWeb默认配置去下载,下载回来的HTML代码是乱码,应该进行如下操作 HtmlWeb web = new Html ...
- winform客户端向web地址传参,怎样去接收参数。
在web端定义js方法去接收客户端传递过来的参数,具体就是获取地址中?后的数据,各个参数用&分割,存储于数组中,获取. 具体如下: //定义获取地址中参数的方法 function GetReq ...
- 第五百七十八、九天 how can I 坚持
这样下去不行啊 ,昨天晚上回来捣鼓了一晚上手机,看个视频还经常开小差,得全力以赴了,不能抱着打酱油的心态了,加油. 今天和yj聊了聊,好多事啊,不能一心工作了,还得考虑结婚,也是醉了. 努力吧,先把考 ...
- Java web--反射(解刨)
本质:先加载类 再解刨类的方法,字段,构造函数 目的:解刨出构造函数 为了new对象 解刨出字段 为了封装数据进去 解刨方法 ...