后端开发|C#.Net教程
C#,验证码,创建与使用
后端开发-C#.Net教程1、C#创建验证码
订餐php cms源码,ubuntu重置密码器,爬虫网易微专业,分库php,合肥seo学习lzw
① 创建获取验证码页面(ValidateCode.aspx)
房产公司网站源码,Ubuntu复制,爬虫 请求 设置编码,淡水php,天津seo实战lzw
获取验证码
idc销售主机源码,vscode代码适应屏幕,求字体ubuntu,tomcat 假死状态,安卓sqlite表内数据,怀孕梦到黑色的爬虫是什么,php 判断网络,蜘蛛屯兰州seo公司,如何制作一元夺宝网站源码,网页游戏无限元宝吧,织梦 绿色风格模板lzw
② 编写获取验证码代码(ValidateCode.aspx.cs)
/// private string validateCodeType = "0";/// private int validateCodeCount = 4;/// char[] character = { 2, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F, G, H, J, K, L, M, N, P, R, S, T, W, X, Y };protected void Page_Load(object sender, EventArgs e){ //取消缓存 Response.BufferOutput = true; Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1)); Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); Response.AppendHeader("Pragma", "No-Cache"); //获取设置参数 if (!string.IsNullOrEmpty(Request.QueryString["validateCodeType"])) { validateCodeType = Request.QueryString["validateCodeType"]; } if (!string.IsNullOrEmpty(Request.QueryString["validateCodeCount"])) { int.TryParse(Request.QueryString["validateCodeCount"], out validateCodeCount); } //生成验证码 this.CreateCheckCodeImage(GenerateCheckCode());}private string GenerateCheckCode(){ char code ; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < validateCodeCount; i++) { code = character[random.Next(character.Length)]; // 要求全为数字或字母 if (validateCodeType == "1") {if ((int)code 57){ i--; continue;} } else if (validateCodeType == "2") {if ((int)code 90){ i--; continue;} } checkCode += code; } Response.Cookies.Add(new System.Web.HttpCookie("CheckCode", checkCode)); this.Session["CheckCode"] = checkCode; return checkCode;}private void CreateCheckCodeImage(string checkCode){ if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length*15.0+40)), 23); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(System.Drawing.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 System.Drawing.Pen(System.Drawing.Color.Silver), x1, y1, x2, y2); } System.Drawing.Font font = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(newSystem.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true); int cySpace = 16; for (int i = 0; i < validateCodeCount; i++) {g.DrawString(checkCode.Substring(i, 1), font, brush, (i + 1) * cySpace, 1); } //画图片的前景噪音点 for (int i = 0; i < 100; i++) {int x = random.Next(image.Width);int y = random.Next(image.Height);image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); }}
2、验证码的使用
① 验证码的前段显示代码
③ 编写验证码测试的提交代码(ValidateTest.aspx.cs)
protected void btnVal_Click(object sender, EventArgs e){ bool result = false; //验证结果 string userCode = this.txtValidate.Value; //获取用户输入的验证码 if (String.IsNullOrEmpty(userCode)) { //请输入验证码 return; } string validCode = this.Session["CheckCode"] as String; //获取系统生成的验证码 if (!string.IsNullOrEmpty(validCode)) { if (userCode.ToLower() == validCode.ToLower()) {//验证成功result = true; } else {//验证失败result = false; } }}