zoukankan      html  css  js  c++  java
  • (一)学习MVC之制作验证码

    制作验证码的方法在@洞庭夕照 看到的,原文链接:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html

    现自己利用该方法制作一个简单的验证码,运行程序效果如下:

    现正式开始:

    1.新建项目:VerificationCodeTest

    2.选择模板:基本

    3.添加图片:

    放置位置:

    4.重点来了,新建文件夹Image,再新建一个类Text.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web;
    
    namespace VerificationCodeTest.Common
    {
        public class Text
        {
            /// <summary>
            /// 获取验证码【字符串】
            /// </summary>
            /// <param name="Length">验证码长度必须大于0</param>
            /// <returns></returns>
            public static string VerificationText(int Length)
            {
                char[] _verification = new Char[Length];//创建类型为char的一维数组_verification,数量为Length个。
                Random _random = new Random(); //实例化一个Random类的对象_random.
                char[] _dictionary ={//创建类型为char的一维数组_dictionary
                                       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
                                   };
                for (int i = 0; i < Length; i++)
                {
                    _verification[i] = _dictionary[_random.Next(_dictionary.Length - 1)];//Random方法 random.Next(3)表示返回一个范围是[0,3)的随机数,random.Next(3,9)表示返回一个范围是[3,9)的随机数.
                }
                return new string(_verification);//返回新的字符串_verification(数组组成字符串)
            }
        }
    }

    5.添加控制器:Control/HomeControl.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace VerificationCodeTest.Controllers
    {
    
    
        using System.Drawing;
        using System.Drawing.Drawing2D;
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Index()
            {
                return View();
            }
            /// <summary>
            /// 绘制验证码
            /// </summary>
            /// <returns></returns>
            public ActionResult VerificationCode()
            {
                int _verificationLength = 6; //定义一个整数变量,作为验证码的长度。
                int _width = 100, _height = 20;//验证码背景大小?
                SizeF _verificationTextSize;//存储有序浮点数对,通常为矩形的宽度和高度。 SizeF 结构的 Height 和 Width 的单位取决于用于绘制的 Graphics 对象的 PageUnit 和 PageScale 设置。
                Bitmap _bitmap = new Bitmap(Server.MapPath("~/Content/Common/Texture.jpg"), true);// Bitmap 是用于处理由像素数据定义的图像的对象。
    
                TextureBrush _brush = new TextureBrush(_bitmap);//TextureBrush 类的每个属性都是 Brush 对象,这种对象使用图像来填充形状的内部。 此类不能被继承。
    
                //获取验证码
                string _verificationText = Common.Text.VerificationText(_verificationLength);//得到随机产生包含6个由数字和字符组成的字符串
                //存储验证码
                Session["VerificationCode"] = _verificationText.ToUpper();//把字符转化成对应的大写字母,储存在Session对象的VerificationCode变量中
                //Session 对象用于存储关于用户的信息,或者为一个用户的 session 更改设置。存储于 session 对象中的变量存有单一用户的信息,并且对于应用程序中的所有页面都是可用的。
                Font _font = new Font("Arial", 14, FontStyle.Bold);
                Bitmap _image = new Bitmap(_width, _height);
                Graphics _g = Graphics.FromImage(_image);//从指定的 Image 创建新的 Graphics。
                //清空背景色
                _g.Clear(Color.White);
                //绘制验证码
                _verificationTextSize = _g.MeasureString(_verificationText, _font);//测量用指定的 _font 绘制的指定字符串_verificationText。
                _g.DrawString(_verificationText, _font, _brush, (_width - _verificationTextSize.Width) / 2, (_height - _verificationTextSize.Height) / 2);
                _image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将该 Image 保存到指定的文件或流。
                return null;
            }
    
        }
    }

    6.添加视图:Index

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <dd>
            <div class="label">验证码:</div>
            <div class="ctrl">
                @*@Html.TextBoxFor(model => model.VerificationCode)
                    @Html.ValidationMessageFor(model => model.VerificationCode)*@
                <img id="v" alt="" src="@Url.Action("VerificationCode", "Home")" />
                <a id="t" style="cursor:pointer">换一张</a>
            </div>
        </dd>
    }
    <style type="text/css">
        .label.highlight {
            color: red;
        }
    </style>
    
    
    @section Scripts {
        <script src="~/Scripts/jquery-1.8.2.js"></script>
        <script type="text/javascript">
    
            $("#t").click(function () {
                $("#v").attr("src", "/Home/VerificationCode?" + new Date());
            })
        </script>
    }

    7.最后运行程序

    8.结束。

  • 相关阅读:
    jquery插件开发
    五种常见的 PHP 设计模式
    linux常用命令
    解决MySQL不允许从远程访问的方法
    模块化的JavaScript开发的优势在哪里
    巧用C#做中间语言 实现Java调用.net DLL
    PHP Predefined Interfaces 预定义接口
    想追赶.Net的脚步?Java面前障碍重重
    修改一行SQL代码 性能提升了N倍
    如何使用LoadRunner监控Windows
  • 原文地址:https://www.cnblogs.com/wiming/p/3930450.html
Copyright © 2011-2022 走看看