前台
<img id="vcodeimg" src="/Home/VCode" width="70" height="25" /> <span style="cursor: pointer; text-decoration: underline">换一张</span>
控制器
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Utility; using Jellal**; namespace sjlwebsite.Controllers { public class CommonController : Controller { #region 验证码 [OutputCache(Duration = 0)] public ActionResult VCode() { string code = ValidateCode.CreateRandomCode(4); Session["vcode"] = code; ValidateCode.CreateImage(code); return View(); } public string GetCode() { return Session["vcode"].ToStr(); } #endregion } }
验证码类
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Utility { /// <summary> /// 完美随机验证码 0.10 /// Verion:0.10 /// Description:随机生成设定验证码,并随机旋转一定角度,字体颜色不同 /// </summary> public class ValidateCode { /// <summary> /// 生成随机码 /// </summary> /// <param name="length">随机码个数www.52mvc.com</param> /// <returns></returns> public static string CreateRandomCode(int length) { int rand; char code; string randomcode = String.Empty; //生成一定长度的验证码 System.Random random = new Random(); for (int i = 0; i < length; i++) { rand = random.Next(); if (rand % 3 == 0) { code = (char)('A' + (char)(rand % 26)); } else { code = (char)('0' + (char)(rand % 10)); } randomcode += code.ToString(); } return randomcode; } /// <summary> /// 创建随机码图片 /// </summary> /// <param name="randomcode">随机码</param> public static void CreateImage(string randomcode) { int randAngle = 45; //随机转动角度 int mapwidth = (int)(randomcode.Length * 23); Bitmap map = new Bitmap(mapwidth, 28);//创建图片背景 Graphics graph = Graphics.FromImage(map); graph.Clear(Color.AliceBlue);//清除画面,填充背景 graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框 //graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式 Random rand = new Random(); //背景噪点生成 www.jb51.net Pen blackPen = new Pen(Color.LightGray, 0); for (int i = 0; i < 50; i++) { int x = rand.Next(0, map.Width); int y = rand.Next(0, map.Height); graph.DrawRectangle(blackPen, x, y, 1, 1); } //验证码旋转,防止机器识别 char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组 //文字距中 StringFormat format = new StringFormat(StringFormatFlags.NoClip); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; //定义颜色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //定义字体 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; for (int i = 0; i < chars.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);//字体样式(参数2为字体大小) Brush b = new System.Drawing.SolidBrush(c[cindex]); Point dot = new Point(16, 16); //graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150);//测试X坐标显示间距的 float angle = rand.Next(-randAngle, randAngle);//转动的度数 graph.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置 graph.RotateTransform(angle); graph.DrawString(chars.ToString(), f, b, 1, 1, format); //graph.DrawString(chars.ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format); graph.RotateTransform(-angle);//转回去 graph.TranslateTransform(2, -dot.Y);//移动光标到指定位置 } //graph.DrawString(randomcode,fontstyle,new SolidBrush(Color.Blue),2,2); //标准随机码 //生成图片 System.IO.MemoryStream ms = new System.IO.MemoryStream(); map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/gif"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); graph.Dispose(); map.Dispose(); } } }
更多ASP.NET MVC验证码功能实现代码相关文章请关注PHP中文网!

C#.NET是一个强大的开发平台,结合了C#语言和.NET框架的优势。1)它广泛应用于企业应用、Web开发、游戏开发和移动应用开发。2)C#代码编译成中间语言后由.NET运行时环境执行,支持垃圾回收、类型安全和LINQ查询。3)使用示例包括基本控制台输出和高级LINQ查询。4)常见错误如空引用和类型转换错误可以通过调试器和日志记录解决。5)性能优化建议包括异步编程和优化LINQ查询。6)尽管面临竞争,C#.NET通过不断创新保持其重要地位。

C#.NET的未来趋势主要集中在云计算、微服务、AI和机器学习集成以及跨平台开发三个方面。1)云计算和微服务:C#.NET通过Azure平台优化云环境表现,支持构建高效微服务架构。2)AI和机器学习集成:借助ML.NET库,C#开发者可在应用中嵌入机器学习模型,推动智能化应用发展。3)跨平台开发:通过.NETCore和.NET5 ,C#应用可在Windows、Linux和macOS上运行,扩展部署范围。

C#.NET开发的最新动态和最佳实践包括:1.异步编程提高应用响应性,使用async和await关键字简化非阻塞代码;2.LINQ提供强大查询功能,通过延迟执行和表达式树高效操作数据;3.性能优化建议包括使用异步编程、优化LINQ查询、合理管理内存、提升代码可读性和维护性、以及编写单元测试。

如何利用.NET构建应用?使用.NET构建应用可以通过以下步骤实现:1)了解.NET基础知识,包括C#语言和跨平台开发支持;2)学习核心概念,如.NET生态系统的组件和工作原理;3)掌握基本和高级用法,从简单控制台应用到复杂的WebAPI和数据库操作;4)熟悉常见错误与调试技巧,如配置和数据库连接问题;5)应用性能优化与最佳实践,如异步编程和缓存。

C#在企业级应用、游戏开发、移动应用和Web开发中均有广泛应用。1)在企业级应用中,C#常用于ASP.NETCore开发WebAPI。2)在游戏开发中,C#与Unity引擎结合,实现角色控制等功能。3)C#支持多态性和异步编程,提高代码灵活性和应用性能。

C#和.NET适用于Web、桌面和移动开发。1)在Web开发中,ASP.NETCore支持跨平台开发。2)桌面开发使用WPF和WinForms,适用于不同需求。3)移动开发通过Xamarin实现跨平台应用。

C#.NET生态系统提供了丰富的框架和库,帮助开发者高效构建应用。1.ASP.NETCore用于构建高性能Web应用,2.EntityFrameworkCore用于数据库操作。通过理解这些工具的使用和最佳实践,开发者可以提高应用的质量和性能。

如何将C#.NET应用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。1.在Azure上,使用AzureAppService和AzurePipelines自动化部署。2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda实现部署和无服务器计算。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6
视觉化网页开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。