【蛤蟆】頁面驗證碼程式(C#)
在.NET中新建一個Web專案,新增一個.ASPX頁面,取名VerifyCode.aspx, 轉到其程式碼編輯狀態,將下面的程式碼Copy&Paste過去就可以用了,有什麼意見或問題歡迎提出^@^
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Text;
namespace VerifyCode
{
/**////
/// 頁面驗證碼程式
/// 使用:在頁面中加入HTML程式碼
///
public class VerifyCode : System.Web.UI.Page
{
Private Members#region Private Members
static string[] FontItems = new string[] { "Arial",
"Helvetica",
"Geneva",
"sans-serif",
"Verdana"
};
static Brush[] BrushItems = new Brush[] { Brushes.OliveDrab,
Brushes.ForestGreen,
Brushes.DarkCyan,
Brushes.LightSlateGray,
Brushes.RoyalBlue,
Brushes.SlateBlue,
Brushes.DarkViolet,
Brushes.MediumVioletRed,
Brushes.IndianRed,
Brushes.Firebrick,
Brushes.Chocolate,
Brushes.Peru,
Brushes.Goldenrod
};
static string[] BrushName = new string[] { "OliveDrab",
"ForestGreen",
"DarkCyan",
"LightSlateGray",
"RoyalBlue",
"SlateBlue",
"DarkViolet",
"MediumVioletRed",
"IndianRed",
"Firebrick",
"Chocolate",
"Peru",
"Goldenrod"
};
private static Color BackColor = Color.White;
private static Pen BorderColor = Pens.DarkGray;
private static int Width = 52;
private static int Height = 21;
private Random _random;
private string _code;
private int _brushNameIndex;
#endregion // end of Private Members
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
PageLoad Event#region PageLoad Event
public void Page_Load(object sender, System.EventArgs e)
{
//
// TODO : initialize
//
this._random = new Random();
this._code = GetRandomCode();
//
// TODO : use Session["code"] save the VerifyCode
//
Session["code"] = this._code;
//
// TODO : output Image
//
this.SetPageNoCache();
this.OnPaint();
}
#endregion // end of PageLoad Event
Helper Methods#region Helper Methods
/**////
/// 設定頁面不被快取
///
private void SetPageNoCache()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma","No-Cache");
}
/**////
/// 取得一個 4 位的隨機碼
///
///
private string GetRandomCode()
{
return Guid.NewGuid().ToString().Substring(0, 4);
}
/**////
/// 隨機取一個字型
///
///
private Font GetFont()
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Text;
namespace VerifyCode
{
/**////
/// 頁面驗證碼程式
/// 使用:在頁面中加入HTML程式碼
///
public class VerifyCode : System.Web.UI.Page
{
Private Members#region Private Members
static string[] FontItems = new string[] { "Arial",
"Helvetica",
"Geneva",
"sans-serif",
"Verdana"
};
static Brush[] BrushItems = new Brush[] { Brushes.OliveDrab,
Brushes.ForestGreen,
Brushes.DarkCyan,
Brushes.LightSlateGray,
Brushes.RoyalBlue,
Brushes.SlateBlue,
Brushes.DarkViolet,
Brushes.MediumVioletRed,
Brushes.IndianRed,
Brushes.Firebrick,
Brushes.Chocolate,
Brushes.Peru,
Brushes.Goldenrod
};
static string[] BrushName = new string[] { "OliveDrab",
"ForestGreen",
"DarkCyan",
"LightSlateGray",
"RoyalBlue",
"SlateBlue",
"DarkViolet",
"MediumVioletRed",
"IndianRed",
"Firebrick",
"Chocolate",
"Peru",
"Goldenrod"
};
private static Color BackColor = Color.White;
private static Pen BorderColor = Pens.DarkGray;
private static int Width = 52;
private static int Height = 21;
private Random _random;
private string _code;
private int _brushNameIndex;
#endregion // end of Private Members
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
PageLoad Event#region PageLoad Event
public void Page_Load(object sender, System.EventArgs e)
{
//
// TODO : initialize
//
this._random = new Random();
this._code = GetRandomCode();
//
// TODO : use Session["code"] save the VerifyCode
//
Session["code"] = this._code;
//
// TODO : output Image
//
this.SetPageNoCache();
this.OnPaint();
}
#endregion // end of PageLoad Event
Helper Methods#region Helper Methods
/**////
/// 設定頁面不被快取
///
private void SetPageNoCache()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma","No-Cache");
}
/**////
/// 取得一個 4 位的隨機碼
///
///
private string GetRandomCode()
{
return Guid.NewGuid().ToString().Substring(0, 4);
}
/**////
/// 隨機取一個字型
///
///
private Font GetFont()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-349317/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 同一頁面多次呼叫驗證碼
- C# 滑動驗證碼|拼圖驗證|SlideCaptchaC#IDEAPT
- 識別英文數字驗證碼的程式(C# 示例)C#
- 記在dcat-admin中,登陸頁面加驗證碼操作
- C# 常用驗證C#
- 頁面常用程式碼整理
- 使用 C# 識別英文數字驗證碼C#
- 免費404頁面程式碼分享 404錯誤頁面原始碼原始碼
- volatile的特性程式碼驗證
- 同一頁面生成多個驗證碼
- JavaScript頁面跳轉程式碼JavaScript
- Java製作驗證碼的完整程式碼Java
- 驗證碼原理及驗證
- C#滑動拼圖驗證碼實現筆記C#筆記
- 驗證碼---js重新整理驗證碼JS
- C#釋出程式:應用程式驗證未成功,無法繼續C#
- JavaScript 表單驗證程式碼例項JavaScript
- 分享一款漂亮的 C# .Net 圖形驗證碼C#
- JavaScript驗證碼生成和驗證效果JavaScript
- 如何實現 mysql 匯出資料,驗證頁面正確性?MySql
- 驗證碼機制之驗證碼重複使用
- 登入驗證碼生成kaptcha(輸入驗證碼)APT
- 小程式 - 驗證碼倒數計時元件元件
- Laravel - 驗證碼Laravel
- Response驗證碼
- PHP驗證碼PHP
- 【驗證碼逆向專欄】某多多驗證碼逆向分析
- easy-captcha實現驗證碼驗證APT
- 影片直播app原始碼,傳送驗證碼 驗證碼識別APP原始碼
- 程式碼簽名證書是如何進行驗證工作的
- 說說如何在登入頁實現生成驗證碼功能
- PHP 驗證身份證號碼PHP
- 某滑塊驗證碼識別思路(附完整程式碼)
- 《神經網路的梯度推導與程式碼驗證》之LSTM前向和反向傳播的程式碼驗證神經網路梯度反向傳播
- antd頁面多表單校驗
- Win32 C++程式碼快速驗證模板Win32C++
- 常見驗證碼的弱點與驗證碼識別
- 【轉】改變檢視頁面原始碼的程式原始碼
- 如何透過Python程式碼旋轉PDF頁面Python