简单C#、asp.net mvc验证码的实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Drawing;
using System.IO;
namespace 验证码的实现.ValidateCode
{
/// <summary>
/// 验证码生成工具类
/// </summary>
///
public class ValidateCodeHelper
{
private static Random rand = new Random();
private static string code;
/// <summary>
/// 随机生成指定长度的验证码
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GetCode(int length) {
string codes = "AaBbCcDdEeFfJjHhIiJjKkMmNnPpQrRSsTtUuVvWwXxYyZz0123456789";
StringBuilder sb = new StringBuilder();
for (int i = 0; i <length; i++)
{
int index=rand.Next(codes.Length);
if (sb.ToString().Contains(codes[index])) {
i--;
continue;
}
sb.Append(codes[index]);
}
code = sb.ToString();
return code;
}
/// <summary>
/// 获取随机颜色
/// </summary>
/// <returns></returns>
private static Color GetRandomColor() {
int red = rand.Next(10, 255);
int green = rand.Next(10, 255);
int blue = rand.Next(10, 255);
return Color.FromArgb(red, green, blue);
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
public static byte[] ValidateCode(string code) {
Bitmap img = new Bitmap(100,30);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(Brushes.White, 0, 0, img.Width, img.Height);
g.DrawRectangle(new Pen(Color.Black), 1, 1, img.Width-2, img.Height-2);
Brush bush = new SolidBrush(Color.SteelBlue);
g.DrawString(code, new Font("黑体", 20, FontStyle.Italic), bush, 10, 2);
Random r = new Random();
//画线条
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(GetRandomColor()), r.Next(img.Width), r.Next(img.Height), r.Next(img.Width), r.Next(img.Height));
}
//画躁点
for (int i = 0; i < 100; i++)
{
img.SetPixel(r.Next(img.Width), r.Next(img.Height), GetRandomColor());
}
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
g.Dispose();
ms.Close();
return data;
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="code">验证码</param>
/// <param name="fontColor">验证码颜色</param>
/// <returns></returns>
public static byte[] ValidateCode(string code,Color fontColor) {
Bitmap img = new Bitmap(100,30);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(Brushes.White,0, 0, img.Width, img.Height);
Brush bush = new SolidBrush(fontColor);
g.DrawString(code, new Font("黑体", 20, FontStyle.Italic), bush, 10, 2);
Random r = new Random();
//画线条
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(GetRandomColor()), r.Next(img.Width), r.Next(img.Height), r.Next(img.Width), r.Next(img.Height));
}
//画躁点
for (int i = 0; i < 100; i++)
{
img.SetPixel(r.Next(img.Width), r.Next(img.Height), GetRandomColor());
}
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
g.Dispose();
ms.Close();
return data;
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="code">验证码</param>
/// <param name="fontColor">验证码颜色</param>
/// <param name="backgroundColor">验证码背景颜色</param>
/// <returns></returns>
public static byte[] ValidateCode(string code,Color backgroundColor, Color fontColor)
{
Bitmap img = new Bitmap(100, 30);
Graphics g = Graphics.FromImage(img);
Brush bush1 = new SolidBrush(backgroundColor);
g.FillRectangle(bush1, 0, 0, img.Width, img.Height);
Brush bush = new SolidBrush(fontColor);
g.DrawString(code, new Font("黑体", 20, FontStyle.Italic), bush, 10, 2);
Random r = new Random();
//画线条
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(GetRandomColor()), r.Next(img.Width), r.Next(img.Height), r.Next(img.Width), r.Next(img.Height));
}
//画躁点
for (int i = 0; i < 100; i++)
{
img.SetPixel(r.Next(img.Width), r.Next(img.Height), GetRandomColor());
}
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
g.Dispose();
ms.Close();
return data;
}
/// <summary>
/// 判断验证码是否正确
/// </summary>
/// <param name="Code"></param>
/// <returns></returns>
public static bool IsValidate(string Code) {
if (string.IsNullOrEmpty(Code)||!code.ToLower().Equals(Code.ToLower())) {
return false;
}
return true;
}
}
}
在控制器中的调用
public ActionResult ValidateCode(){
//获取指定长度验证码
string code= ValidateCodeHelper.GetCode(5);
TempData["code"] = code;//存储验证码用于验证
//将验证码绘制到图片上、保存到内存流中并返回字节数组
byte[] data= ValidateCodeHelper.ValidateCode(code);
return File(data,"image/jpeg");
}
在前端的调用
<script>
function change() {
var img = document.getElementsByTagName('img')[0];
img.src = img.src + "?";
}
</script>
<form method="post" action="/Home/Login">
<table>
<tr>
<td>验证码:</td>
<td><img src="/Home/ValidateCode" style="cursor:pointer" onclick="this.src =this.src+'?'" />
<a href="javascript:void(0)" onclick="change()">换一张</a>
</td>
</tr>
<tr>
<td>输入验证码:</td>
<td><input type="text" name="code"/></td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
简单C#、asp.net mvc验证码的实现的更多相关文章
- ASP.NET MVC验证码演示(Ver2)
前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...
- ASP.NET MVC验证码演示
我们在网站登录或理一个评论时,可以放置一个验证码(Captcha),可以为系统免去那些恶意刷新等功能. 今次Insus.NET在asp.net mvc应用程序实现与演示验证码的产生以及应用等 . 前天 ...
- ASP.NET mvc 验证码 (转)
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
- 简单的ASP.NET MVC发布
学习这样久的ASP.NET MVC,但一直没有实现过发布MVC程序.今天来试试. 分两个部分进行,先是第一部分,Visual Studio的publish:创建一个带有实例的ASP.NET MVC: ...
- 一个简单的ASP.NET MVC异常处理模块
一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行.异常处理应该是一个横切点,所谓横切点就是各个部分都 ...
- asp.net mvc 验证码
效果图 验证码类 namespace QJW.VerifyCode { //用法: //public FileContentResult CreateValidate() //{ // Validat ...
- 一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。
前言 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架 ...
- 简单实现ASP.Net MVC网页播放音乐
<div> @*音乐*@ <audio id="warning-sound" loop="loop" src="/Areas/Map ...
- Log4net入门(ASP.NET MVC 5篇)
在前4篇Log4net入门文章中,我们讲述了log4net的一些简单用法,在这一篇中我们主要讲述如何在ASP.NET MVC 5项目中将日志信息写入SQL Server数据库中. 一.创建最简单的AS ...
随机推荐
- poj 3614
http://poj.org/problem?id=3614 题意:有n头奶牛想要晒太阳,但他们每个人对太阳都有不同的耐受程度,也就是说,太阳不能太大也不能太小,现在有一种防晒霜,涂抹这个防晒霜可以把 ...
- raspbian调整键盘设置
参考 http://www.jianshu.com/p/8c474339a238 树莓派(raspberry pi)是英国产品,默认键盘布局是英国(GB),我们用的键盘布局一般是美国(US)的(104 ...
- css-关于文本
1. 使用 text-overflow:ellipsis; 超出部分会变成省略号 http://www.w3school.com.cn/tiy/t.asp?f=css3_text-overflow 1 ...
- int ,long , long long类型的范围
int ,long , long long类型的范围 unsigned - - unsigned - - unsigned __int64的最大值: __int64的最小值:- unsigned __ ...
- Mac地址
Mac地址是每张网卡的唯一标识符,也叫物理地址.硬件地址或链路地址,由网络设备制造商生产时烧在网卡的ROM中,可以修改.现在的Mac地址一般都采用6字节48bit(还有2字节16bit的Mac地址,多 ...
- @rpath/libswiftCore.dylib问题
dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/containers/Bundle ...
- 解决eclipse中svn插件总是提示输入密码的问题
一.背景 最近在eclipse中使用svn插件进行远程仓库代码管理时,老是出现提示让输入密码,特别烦人,经过努力,终于解决该问题,拿来和大家分享~ 二.svn插件密码机制以及出现问题的原因分析 当我们 ...
- iOS系统验证关闭
在浏览器上查看iOS系统与否方法:1.打开浏览器2.在地址栏中输入 ipsw.me3.在打开的网页中选择 Select a device 选择你要查看的设备型号:4.选择好设备之后点击select i ...
- File System的简单操作
在进行这些操作之前,需要在js文件中导入fs模块 const fs = require("fs"); const是定义一个常量,比较特殊的是,使用const定义时必须赋值,一旦被赋 ...
- JAVA程序中SQL语句无法传递中文参数
vi /etc/my.cnf [mysqld]# The default character set that will be used when a new schema or table is# ...