Dieser Artikel ist ein Spiegelartikel der maschinellen Übersetzung, bitte klicken Sie hier, um zum Originalartikel zu springen.

Ansehen: 14659|Antwort: 0

[ASP.NET] ASPX vs. MVC Page Captcha

[Link kopieren]
Veröffentlicht am 24.12.2015 12:31:37 | | |
CAPTCHA-Code


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;

  11. namespace Itcast.CMS.Common
  12. {
  13.    public class ValidateCode
  14.   {
  15.     public ValidateCode()
  16.     {
  17.     }
  18.     /// <summary>
  19.     /// 验证码的最大长度
  20.     /// </summary>
  21.     public int MaxLength
  22.     {
  23.       get { return 10; }
  24.     }
  25.     /// <summary>
  26.     /// 验证码的最小长度
  27.     /// </summary>
  28.     public int MinLength
  29.     {
  30.       get { return 1; }
  31.     }
  32.     /// <summary>
  33.     /// 生成验证码
  34.     /// </summary>
  35.     /// <param name="length">指定验证码的长度</param>
  36.     /// <returns></returns>
  37.     public string CreateValidateCode(int length)
  38.     {
  39.       int[] randMembers = new int[length];
  40.       int[] validateNums = new int[length];
  41.       string validateNumberStr = "";
  42.       //生成起始序列值
  43.       int seekSeek = unchecked((int)DateTime.Now.Ticks);
  44.       Random seekRand = new Random(seekSeek);
  45.       int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
  46.       int[] seeks = new int[length];
  47.       for (int i = 0; i < length; i++)
  48.       {
  49.         beginSeek += 10000;
  50.         seeks[i] = beginSeek;
  51.       }
  52.       //生成随机数字
  53.       for (int i = 0; i < length; i++)
  54.       {
  55.         Random rand = new Random(seeks[i]);
  56.         int pownum = 1 * (int)Math.Pow(10, length);
  57.         randMembers[i] = rand.Next(pownum, Int32.MaxValue);
  58.       }
  59.       //抽取随机数字
  60.       for (int i = 0; i < length; i++)
  61.       {
  62.         string numStr = randMembers[i].ToString();
  63.         int numLength = numStr.Length;
  64.         Random rand = new Random();
  65.         int numPosition = rand.Next(0, numLength - 1);
  66.         validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
  67.       }
  68.       //生成验证码
  69.       for (int i = 0; i < length; i++)
  70.       {
  71.         validateNumberStr += validateNums[i].ToString();
  72.       }
  73.       return validateNumberStr;
  74.     }
  75.     /// <summary>
  76.     /// 创建验证码的图片
  77.     /// </summary>
  78.     /// <param name="context">要输出到的page对象</param>
  79.     /// <param name="validateNum">验证码</param>
  80.     public void CreateValidateGraphic(string validateCode, HttpContext context)
  81.     {
  82.       Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
  83.       Graphics g = Graphics.FromImage(image);
  84.       try
  85.       {
  86.         //生成随机生成器
  87.         Random random = new Random();
  88.         //清空图片背景色
  89.         g.Clear(Color.White);
  90.         //画图片的干扰线
  91.         for (int i = 0; i < 25; i++)
  92.         {
  93.           int x1 = random.Next(image.Width);
  94.           int x2 = random.Next(image.Width);
  95.           int y1 = random.Next(image.Height);
  96.           int y2 = random.Next(image.Height);
  97.           g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  98.         }
  99.         Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
  100.         LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
  101.          Color.Blue, Color.DarkRed, 1.2f, true);
  102.         g.DrawString(validateCode, font, brush, 3, 2);
  103.         //画图片的前景干扰点
  104.         for (int i = 0; i < 100; i++)
  105.         {
  106.           int x = random.Next(image.Width);
  107.           int y = random.Next(image.Height);
  108.           image.SetPixel(x, y, Color.FromArgb(random.Next()));
  109.         }
  110.         //画图片的边框线
  111.         g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  112.         //保存图片数据
  113.         MemoryStream stream = new MemoryStream();
  114.         image.Save(stream, ImageFormat.Jpeg);
  115.         //输出图片流
  116.         context.Response.Clear();
  117.         context.Response.ContentType = "image/jpeg";
  118.         context.Response.BinaryWrite(stream.ToArray());
  119.       }
  120.       finally
  121.       {
  122.         g.Dispose();
  123.         image.Dispose();
  124.       }
  125.     }
  126.     /// <summary>
  127.     /// 得到验证码图片的长度
  128.     /// </summary>
  129.     /// <param name="validateNumLength">验证码的长度</param>
  130.     /// <returns></returns>
  131.     public static int GetImageWidth(int validateNumLength)
  132.     {
  133.       return (int)(validateNumLength * 12.0);
  134.     }
  135.     /// <summary>
  136.     /// 得到验证码的高度
  137.     /// </summary>
  138.     /// <returns></returns>
  139.     public static double GetImageHeight()
  140.     {
  141.       return 22.5;
  142.     }



  143.     //C# MVC 升级版
  144.     /// <summary>
  145.     /// 创建验证码的图片
  146.     /// </summary>
  147.     /// <param name="containsPage">要输出到的page对象</param>
  148.     /// <param name="validateNum">验证码</param>
  149.     public byte[] CreateValidateGraphic(string validateCode)
  150.     {
  151.       Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
  152.       Graphics g = Graphics.FromImage(image);
  153.       try
  154.       {
  155.         //生成随机生成器
  156.         Random random = new Random();
  157.         //清空图片背景色
  158.         g.Clear(Color.White);
  159.         //画图片的干扰线
  160.         for (int i = 0; i < 25; i++)
  161.         {
  162.           int x1 = random.Next(image.Width);
  163.           int x2 = random.Next(image.Width);
  164.           int y1 = random.Next(image.Height);
  165.           int y2 = random.Next(image.Height);
  166.           g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  167.         }
  168.         Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
  169.         LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
  170.          Color.Blue, Color.DarkRed, 1.2f, true);
  171.         g.DrawString(validateCode, font, brush, 3, 2);
  172.         //画图片的前景干扰点
  173.         for (int i = 0; i < 100; i++)
  174.         {
  175.           int x = random.Next(image.Width);
  176.           int y = random.Next(image.Height);
  177.           image.SetPixel(x, y, Color.FromArgb(random.Next()));
  178.         }
  179.         //画图片的边框线
  180.         g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  181.         //保存图片数据
  182.         MemoryStream stream = new MemoryStream();
  183.         image.Save(stream, ImageFormat.Jpeg);
  184.         //输出图片流
  185.         return stream.ToArray();
  186.       }
  187.       finally
  188.       {
  189.         g.Dispose();
  190.         image.Dispose();
  191.       }
  192.     }
  193.   }
  194. }
Code kopieren
Controller-Ruf
  1. public ActonResult ShowValidateCode()
  2. {
  3.     ValidateCode validateCode=new ValidateCode();
  4.     string code=validateCode.CreateValidateCode(4);//生成长度为4的验证码
  5.     byte[]buffer=validateCode.CreateValidateGraphic(code);
  6.     return File(buffer, "image/jpeg");
  7. }
Code kopieren
Der Vordergrund-Anzeigecode
  1. <img id="img" src="/Login/ShowValidateCode/?id=1" style="float: left; height: 24px;" />
  2.                                     <div style="float: left; margin-left: 5px; margin-top: 10px;">
  3.                                         <a href="javascrip{过滤}t:void(0)" onclick="changeCheckCode();return false;">看不清,换一张</a>
  4.                                     </div>
Code kopieren
Vordergrund-jquery-Code
  1. function changeCheckCode() {
  2.      $("#img").attr("src",$("#img").attr("src")+1);  //id=2,为了不重复
  3. }
Code kopieren






Vorhergehend:Ein sehr schönes Benachrichtigungsfeld SweetAlert
Nächster:Asp.Net 404-Seiten-Aufbau
Verzichtserklärung:
Alle von Code Farmer Network veröffentlichten Software, Programmiermaterialien oder Artikel dienen ausschließlich Lern- und Forschungszwecken; Die oben genannten Inhalte dürfen nicht für kommerzielle oder illegale Zwecke verwendet werden, andernfalls tragen die Nutzer alle Konsequenzen. Die Informationen auf dieser Seite stammen aus dem Internet, und Urheberrechtsstreitigkeiten haben nichts mit dieser Seite zu tun. Sie müssen die oben genannten Inhalte innerhalb von 24 Stunden nach dem Download vollständig von Ihrem Computer löschen. Wenn Ihnen das Programm gefällt, unterstützen Sie bitte echte Software, kaufen Sie die Registrierung und erhalten Sie bessere echte Dienstleistungen. Falls es eine Verletzung gibt, kontaktieren Sie uns bitte per E-Mail.

Mail To:help@itsvse.com