简单C#、asp.net mvc验证码的实现
2016-06-10 11:29
716 查看
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>
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>
相关文章推荐
- 生成URL(而不是链接) Generating URLs (and Not Links) | 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- 指定HTML标签属性 |Specifying HTML Attributes| 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- 传递额外的值 Passing Extra Values |在视图中生成输出URL | 高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- 用路由系统生成输出URL 在视图中生成输出URL 高级路由特性 精通ASP-NET-MVC-5-弗瑞曼
- 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法
- 运用路由约束 使用属性路由 精通ASP-NET-MVC-5-弗瑞曼
- 使用Route Prefix 使用属性路由 精通ASP-NET-MVC-5-弗瑞曼
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
- 用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
- ASP.NET MVC中的Global.asax文件
- ASP.NET MVC中的Razor语法
- Configuring Autofac to work with the ASP.NET Identity Framework in MVC 5
- 从零开始搭建Raspberry Pi机器视觉编程环境
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- 约束路由 用正则表达式约束路由 Constraining a Route Using a Regular Expression 精通ASP-NET-MVC-5-弗瑞曼
- 性能工具MiniProfiler在Asp.Net WebForm跟踪EntityFramework
- AspNet Identity and IoC Container Registration
- CocoaPods和版本控制小技巧
- asp.net简单网站的实现
- Web Server 在iis下部署asp网站在iis下