MVC的验证码

后台:

 /// <summary>
        /// 创建验证码的图片
        /// </summary>
        /// <param name="validateCode">验证码</param>public ActionResult CreateValidateGraphic(string validateCode)
        {
            Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 14.0), 34);

            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));
                string[] fontName = { "华文新魏", "宋体", "圆体", "黑体", "隶书" };
                Font font = new Font(fontName[new Random().Next(0, validateCode.Length)], 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, 8);
                //画图片的前景干扰点
                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();
                return File(stream.ToArray(), @"image/jpeg");
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

前台调用:

<img src="@Url.Action("CreateValidateGraphic", "Home", new { validateCode = "5678"})" />

参数代表的是验证码内容,可以使用GUID前几位或者随机数代替

时间: 2024-12-13 15:07:03

MVC的验证码的相关文章

(一)【转】asp.net mvc生成验证码

网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中. 在ASP.NET网站中应用验证码是很容易的,网上有很多的解决方案.最近在做一个OA项目,因系统采用的ASP.NET MVC框架,同样在登录页中需用到验证码,故需将原来在ASP.NET网站中使用的验证码移植到ASP.NET MVC中. 原ASP.NET网站用来生成验证码的类文件ValidateCode.

MVC下验证码

验证码技术是目前很多WEB程序采用的一种安全防御技术,主要用于防止使用软件程序自动和批量提交表单.MvcCaptcha是应用于ASP.NET MVC Web应用程序中的验证码控件,功能强大,使用简单方便,生成的验证码图片效果如下: MvcCaptcha的主要特点: 支持自定义验证码图片的字体扭曲程度(None, Low, Medium, High, Extreme五级)和图片中的文本个数: 支持自定义验证码图片的背景杂色级别(None, Low, Medium, High, Extreme五级)

aspx与mvc页面验证码

验证码类代码 using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespac

MVC生成图片验证码,可指定位数

前台: <h2>mvc后台生成验证码,可指定位数</h2> <img id="gc" src="GetValidateCode" onclick="this.src='GetValidateCode?r=' + Math.random()" /> <button onclick="javascript:document.getElementById('gc').src='GetValidateC

spring mvc 页面验证码样例

验证码类: import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public

spring mvc之验证码kaptcha

该验证码使用的谷歌的kaptcha,在他的基础上去掉了模糊的属性,和官方的jar包有区别.不废话,贴代码. <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Conf

asp.net mvc 发送验证码

public ActionResult Index() { /*第一种,利用Google的smtp来发送邮件*/ SmtpClient client = new SmtpClient("smtp.163.com", 25); Random Rdm = new Random(); //产生0到100的随机数 int iRdm = Rdm.Next(99999, 100000); MailMessage msg = new MailMessage("邮箱@163.com"

C#工具:ASP.NET MVC生成图片验证码

1.复制下列代码,拷贝到控制器中. #region 生成验证码图片 // [OutputCache(Location = OutputCacheLocation.None, Duration = 0, NoStore = false)] public ActionResult SecurityCode() { string oldcode = Session["SecurityCode"] as string; string code = CreateRandomCode(5); Se

MVC产生验证码

来源地址: http://www.cnblogs.com/insus/p/3629269.html