欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

ASP.NET

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > ASP.NET >

asp.net MVC 在Controller控制器中實(shí)現(xiàn)驗(yàn)證碼輸出功能

來源:本站原創(chuàng)|時(shí)間:2020-01-11|欄目:ASP.NET|點(diǎn)擊: 次

 asp.net mvc項(xiàng)目使用到驗(yàn)證碼,為了讓以前的WebForm代碼能利用上代碼經(jīng)過稍微的改動(dòng)即可使用代碼如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Angel.Web.Controllers
{
  public class CheckCodeController : Controller
  {
    //
    // GET: /CheckCode/
    public ActionResult Index()
    {
      this.CreateCheckCodeImage(GenerateCheckCode());
      return View();
    }
    private string GenerateCheckCode()
    {
      int number;
      char code;
      string checkCode = String.Empty;
      System.Random random = new Random();
      for (int i = 0; i < 5; i++)
      {
        number = random.Next();
        if (number % 2 == 0)
          code = (char)('0' + (char)(number % 10));
        else
          code = (char)('A' + (char)(number % 26));
        if (code == '0' || code == 'o' || code == 'L' || code == 'I')
        {
          i = i - 1;
        }
        else
        {
          checkCode += code.ToString();
        }
      }
      // Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
      Session.Contents["checkcode"] = checkCode;
      return checkCode;
    }
    private void CreateCheckCodeImage(string checkCode)
    {
      if (checkCode == null || checkCode.Trim() == String.Empty)
        return;
      System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
      Graphics g = Graphics.FromImage(image);
      try
      {
        //生成隨機(jī)生成器
        Random random = new Random();
        //清空?qǐng)D片背景色
        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 System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
        System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
        g.DrawString(checkCode, font, brush, 2, 2);
        //畫圖片的前景噪音點(diǎn)
        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);
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
      }
      finally
      {
        g.Dispose();
        image.Dispose();
      }
    }
  }
}

  最后別忘了session的獲取設(shè)置,需要在Global.asax.cs文件中新增如下代碼:

/// <summary>
/// MVC為了獲取session參數(shù)
/// </summary>
public override void Init()
{
  PostAuthenticateRequest += (s, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
  base.Init();
}
void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
  HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}

html頁面代碼:

html代碼

<img name="img1" id="img1" style="position:absolute;top:5px;right:36px!important;z-index:1000;" alt="單擊圖片刷新驗(yàn)證碼" src="CheckCode/Index" <br>onclick="JavaSccript:reloadImage('CheckCode/Index');" /><br><script type="text/javascript">
function reloadImage(url) {
document.getElementById("img1").src = url + '?abc=' + Math.random();
}
  </script>

總結(jié)

以上所述是小編給大家介紹的asp.net MVC 在Controller控制器中實(shí)現(xiàn)驗(yàn)證碼輸出功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有