asp.net mvc验证码类使用
程序员文章站
2022-05-14 21:20:35
验证码类
namespace qjw.verifycode
{
//用法:
//public filecontentresult createv...
验证码类
namespace qjw.verifycode { //用法: //public filecontentresult createvalidate() //{ // validatecode vcode = new validatecode(); // string code = vcode.createvalidatecode(5); // session["validatecode"] = code; // byte[] bytes = vcode.createvalidategraphic(code); // return file(bytes, "image/jpeg"); //} public class validatecode { public validatecode() { } /// <summary> /// 验证码的最大长度 /// </summary> public int maxlength { get { return 10; } } /// <summary> /// 验证码的最小长度 /// </summary> public int minlength { get { return 1; } } /// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长度</param> /// <returns></returns> public string createvalidatecode(int length) { int[] randmembers = new int[length]; int[] validatenums = new int[length]; string validatenumberstr = ""; //生成起始序列值 int seekseek = unchecked((int)datetime.now.ticks); random seekrand = new random(seekseek); int beginseek = (int)seekrand.next(0, int32.maxvalue - length * 10000); int[] seeks = new int[length]; for (int i = 0; i < length; i++) { beginseek += 10000; seeks[i] = beginseek; } //生成随机数字 for (int i = 0; i < length; i++) { random rand = new random(seeks[i]); int pownum = 1 * (int)math.pow(10, length); randmembers[i] = rand.next(pownum, int32.maxvalue); } //抽取随机数字 for (int i = 0; i < length; i++) { string numstr = randmembers[i].tostring(); int numlength = numstr.length; random rand = new random(); int numposition = rand.next(0, numlength - 1); validatenums[i] = int32.parse(numstr.substring(numposition, 1)); } //生成验证码 for (int i = 0; i < length; i++) { validatenumberstr += validatenums[i].tostring(); } return validatenumberstr; } /// <summary> /// 创建验证码的图片 /// </summary> /// <param name="containspage">要输出到的page对象</param> /// <param name="validatenum">验证码</param> public byte[] createvalidategraphic(string validatecode) { bitmap image = new bitmap((int)math.ceiling(validatecode.length * 12.0), 22); graphics g = graphics.fromimage(image); try { //生成随机生成器 random random = new random(); //清空图片背景色 g.clear(color.white); //画图片的干扰线 for (int i = 0; i < 25; 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.silver), x1, y1, x2, y2); } font font = new font("arial", 12, (fontstyle.bold | fontstyle.italic)); lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true); g.drawstring(validatecode, font, brush, 3, 2); //画图片的前景干扰点 for (int i = 0; i < 100; 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), 0, 0, image.width - 1, image.height - 1); //保存图片数据 memorystream stream = new memorystream(); image.save(stream, imageformat.jpeg); //输出图片流 return stream.toarray(); } finally { g.dispose(); image.dispose(); } } /// <summary> /// 得到验证码图片的长度 /// </summary> /// <param name="validatenumlength">验证码的长度</param> /// <returns></returns> public static int getimagewidth(int validatenumlength) { return (int)(validatenumlength * 12.0); } /// <summary> /// 得到验证码的高度 /// </summary> /// <returns></returns> public static double getimageheight() { return 22.5; } } }
使用方法
public filecontentresult createvalidate() { validatecode vcode = new validatecode(); string code = vcode.createvalidatecode(5); session["validatecode"] = code; byte[] bytes = vcode.createvalidategraphic(code); return file(bytes, "image/jpeg"); }
html
<a href="javascript:;" onclick="reloadcode();"><img id="safecode" src="/common/createvalidate" alt="" /></a>
视图js
function reloadcode() { var verify = document.getelementbyid('safecode'); verify.setattribute('src', '/common/createvalidate?' + math.random()); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: ajax实现远程通信
推荐阅读
-
Spring MVC中使用Google kaptcha验证码的方法详解
-
asp.net下使用DbProviderFactories的数据库操作类
-
从零开始实现ASP.NET Core MVC的插件式开发(一) - 使用ApplicationPart动态加载控制器和视图...
-
Jeditable - jQuery就地编辑插件在ASP.NET MVC 中的使用 JeditablejQuery就地编辑插件ASP.NET MVC使用
-
asp.net 类库中使用ConfigurationManager.ConnectionStrings
-
asp.net 类库中使用ConfigurationManager.ConnectionStrings
-
封装的一个asp.net验证码类
-
asp.net下使用DbProviderFactories的数据库操作类
-
ASP.NET MVC使用EasyUI的datagrid多选提交保存教程
-
封装的一个asp.net验证码类