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.结束。

  • 相关阅读:
    输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少
    有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串
    写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度
    有n个人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位
    有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见图8.43 写一函数实现以上功能,在主函数中输人n个整数和输出调整后的n个数
    输入10个整数,将其中最小的数与第一个数对换, 把最大的数与最后一个数对换
    LOJ#3271. 「JOISC 2020 Day1」建筑装饰 4 DP+找规律
    LOJ#3160. 「NOI2019」斗主地 打表+拉格朗日插值
    CF1386C Joker 双指针+动态树
    LuoguP2605 [ZJOI2010]基站选址 线段树优化DP
  • 原文地址:https://www.cnblogs.com/wiming/p/3930450.html
Copyright © 2011-2022 走看看