ASP.NET驗證碼有哪些-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“ASP.NET驗證碼有哪些”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),義馬企業(yè)網(wǎng)站建設(shè),義馬品牌網(wǎng)站建設(shè),網(wǎng)站定制,義馬網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,義馬網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

1.GSC_WebControlLibrary 這是在網(wǎng)上找到的一個控件,非常好用。但是效果不是特別好(見下圖。
)雖然容易使用,所有的屬性都可以像控件一樣設(shè)置,但是可用性不太高。用戶不能自定義,而且看起來這個驗證碼效果不太好。

效果:

ASP.NET驗證碼有哪些

2.用一個頁面生成圖片,另一個頁面調(diào)用,驗證碼存入cookie,調(diào)用時取cookie對比驗證.這個用戶就可以按自己的喜好更改效果和驗證碼的長度了。

效果如圖:

ASP.NET驗證碼有哪些

CheckCode.aspx代碼如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class Tools_CheckCode : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
  this.CreateCheckCodeImage(GenerateCheckCode());

 }

 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));

   checkCode += code.ToString();
  }

  Response.Cookies.Add(new HttpCookie("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
  {
   //生成隨機生成器
   Random random = new Random();

   //清空圖片背景色
   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.GreenYellow), x1, y1, x2, y2);
   }

   Font font = new System.Drawing.Font("Verdana", 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);

   //畫圖片的前景噪音點
   for (int i = 0; i < 80; 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.Red), 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();
  }
 }


}

然后在需要使用的頁面引用:
UseCheckCode.aspx


<img src="Tools/CheckCode.aspx" alt="驗證碼"  />

3.用web handler生成圖片。這個其實和前面的意思大致差不多,調(diào)用方法也基本和2一樣,不同的是,它的驗證碼是存入Session的。供學(xué)習(xí)參考。

效果圖如下:

ASP.NET驗證碼有哪些

ValidateImageHandler.ashx

%@ WebHandler Language="C#" Class="ValidateImageHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

/// <summary>
/// ValidateImageHandler 生成網(wǎng)站驗證碼功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
{
 int intLength = 5;    //長度
 string strIdentify = "Identify"; //隨機字串存儲鍵值,以便存儲到Session中
 public ValidateImageHandler()
 {  
 }

 /// <summary>
 /// 生成驗證圖片核心代碼
 /// </summary>
 /// <param name="hc"></param>
 public void ProcessRequest(HttpContext hc)
 {
  //設(shè)置輸出流圖片格式
  hc.Response.ContentType = "image/gif";
  
  Bitmap b = new Bitmap(200, 60);
  Graphics g = Graphics.FromImage(b);
  g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
  Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
  Random r = new Random();

  //合法隨機顯示字符列表
  string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  StringBuilder s = new StringBuilder();
  
  //將隨機生成的字符串繪制到圖片上
  for (int i = 0; i < intLength; i++)
  {
   s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
   g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
  }

  //生成干擾線條
  Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
  for (int i = 0; i < 10; i++)
  {
   g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
  }
  b.Save(hc.Response.OutputStream, ImageFormat.Gif);
  hc.Session[strIdentify] = s.ToString(); //先保存在Session中,驗證與用戶輸入是否一致
  hc.Response.End();
 
 }
 
 /// <summary>
 /// 表示此類實例是否可以被多個請求共用(重用可以提高性能)
 /// </summary>
 public bool IsReusable
 {
  get
  {
   return true;
  }
 }
}

“ASP.NET驗證碼有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

本文標(biāo)題:ASP.NET驗證碼有哪些-創(chuàng)新互聯(lián)
本文地址:http://muchs.cn/article6/dcjeig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航商城網(wǎng)站、網(wǎng)站策劃小程序開發(fā)、關(guān)鍵詞優(yōu)化、網(wǎng)站排名

廣告

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

外貿(mào)網(wǎng)站制作