欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

asp.net利用ashx實現(xiàn)驗證碼功能的案例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹了asp.net利用ashx實現(xiàn)驗證碼功能的案例分析,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

為定海等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及定海網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計、成都網(wǎng)站制作、定海網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

驗證碼的好處不用我多說,你們都懂的。我在網(wǎng)上看到有人把驗證碼直接寫在aspx頁面里,也就是說這種方式請求驗證碼等于請求一個頁面,這樣做很不科學(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>

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

下面我介紹一種方式來實現(xiàn)這樣的功能

1. 寫個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$ 的摘要說明
  /// </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位隨機(jī)驗證碼字符
      context.Session["Code"] = checkCode; //將字符串保存到Session中,以便需要時進(jìn)行驗證
      System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);
      Graphics g = Graphics.FromImage(image);
      try
      {
        //生成隨機(jī)生成器
        Random random = new Random();
        //清空圖片背景色
        g.Clear(Color.White);
        // 畫圖片的背景噪音線
        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);
        //畫圖片的前景噪音點(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"; //"或者寫漢字也行"
      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. 在頁面上顯示驗證碼,因為我們生成的是圖形,所以可以直接寫在<img/>標(biāo)簽里,我們只要寫段簡單的腳本就可以點(diǎn)擊鼠標(biāo)切換驗證碼了

<%@ 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(); // 這里加個時間的參數(shù)是為了防止瀏覽器緩存的問題
  }
  </script>
<body>
  <form id="form1" runat="server">
   請輸入驗證碼:<input type="text" name="checknum"/><img src="ashx/doCreateNum.ashx" id="vimg" onclick="changeCode()" />
  </form>
</body>
</html>

說了半天,該是時候看看生成的驗證碼長什么樣了

asp.net利用ashx實現(xiàn)驗證碼功能的案例分析

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享asp.net利用ashx實現(xiàn)驗證碼功能的案例分析內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!

新聞名稱:asp.net利用ashx實現(xiàn)驗證碼功能的案例分析-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article32/coghpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃建站公司云服務(wù)器全網(wǎng)營銷推廣網(wǎng)站排名網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計