国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

如何利用ashx生成圖形驗證碼

發(fā)布時(shí)間:2021-09-04 11:55 來(lái)源:億速云 閱讀:0 作者:chen 欄目: 開(kāi)發(fā)技術(shù)

這篇文章主要講解了“如何利用ashx生成圖形驗證碼”,文中的講解內容簡(jiǎn)單清晰,易于學(xué)習與理解,下面請大家跟著(zhù)小編的思路慢慢深入,一起來(lái)研究和學(xué)習“如何利用ashx生成圖形驗證碼”吧!

本文實(shí)例講述了asp.net使用ashx生成圖形驗證碼的方法。分享給大家供大家參考,具體如下:

驗證碼的好處不用我多說(shuō),你們都懂的。我在網(wǎng)上看到有人把驗證碼直接寫(xiě)在aspx頁(yè)面里,也就是說(shuō)這種方式請求驗證碼等于請求一個(gè)頁(yè)面,這樣做很不科學(xué)。如下所示

<form id="form1" runat="server">
  <p>
    <asp:Image ID="Image1" runat="server" ImageUrl="Default.aspx" />
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
  </p>
</form>

這個(gè)代碼看著(zhù)就覺(jué)得寫(xiě)代碼的人比較欠揍,代碼寫(xiě)成這樣子著(zhù)實(shí)郁悶。驗證嗎也不寫(xiě)點(diǎn)script做下切換。

下面我介紹一種方式來(lái)實(shí)現這樣的功能

1. 寫(xiě)個(gè)ashx生成圖形驗證碼

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.SessionState;
using System.Drawing;
namespace usechecknum.ashx
{
  /// <summary>
  /// $codebehindclassname$ 的摘要說(shuō)明
  /// </summary>
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  public class doCreateNum : IHttpHandler,IRequiresSessionState
  {
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/html";
      string checkCode = GetValidation(5); // 產(chǎn)生5位隨機驗證碼字符
      context.Session["Code"] = checkCode; //將字符串保存到Session中,以便需要時(shí)進(jìn)行驗證
      System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);
      Graphics g = Graphics.FromImage(image);
      try
      {
        //生成隨機生成器
        Random random = new Random();
        //清空圖片背景色
        g.Clear(Color.White);
        // 畫(huà)圖片的背景噪音線(xiàn)
        int i;
        for (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.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);
        //畫(huà)圖片的前景噪音點(diǎn)
        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);
        context.Response.ClearContent();
        context.Response.ContentType = "image/Gif";
        context.Response.BinaryWrite(ms.ToArray());
      }
      finally
      {
        g.Dispose();
        image.Dispose();
      }
    }
    public string GetValidation(int num)
    {
      string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //"或者寫(xiě)漢字也行"
      string validatecode = "";
      Random rd = new Random();
      for (int i = 0; i < num; i++)
      {
        validatecode += str.Substring(rd.Next(0, str.Length), 1);
      }
      return validatecode;
    }
    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}

2. 在頁(yè)面上顯示驗證碼,因為我們生成的是圖形,所以可以直接寫(xiě)在<img/>標簽里,我們只要寫(xiě)段簡(jiǎn)單的腳本就可以點(diǎn)擊鼠標切換驗證碼了

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="usechecknum._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>驗證碼的使用</title>
</head>
<script language="javascript" type="text/javascript">
  function changeCode() {
    var imgNode = document.getElementById("vimg");
    imgNode.src = "ashx/doCreateNum.ashx?t=" + (new Date()).valueOf(); // 這里加個(gè)時(shí)間的參數是為了防止瀏覽器緩存的問(wèn)題
  }
  </script>
<body>
  <form id="form1" runat="server">
   請輸入驗證碼:<input type="text" name="checknum"/><img src="ashx/doCreateNum.ashx" id="vimg" onclick="changeCode()" />
  </form>
</body>
</html>

說(shuō)了半天,該是時(shí)候看看生成的驗證碼長(cháng)什么樣了

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

亚洲国产精品成人综合色| 国语自产拍在线观看2019| 久久 国产 尿 小便 嘘嘘| 狠狠躁夜夜躁AV网站中文字幕| 亚洲欧美日韩综合久久久| 无码成人1000部免费视频|