asp.net中验证码的实现
生活随笔
收集整理的這篇文章主要介紹了
asp.net中验证码的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
主要用到一個類,一個aspx頁面,還有一個使用圖片驗證碼的aspx頁面。 隨機圖片生成類: using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Drawing;
namespace?Sooyie.Common
{
????///?<summary>
????///?產生隨即圖片
????///?</summary>
????public?sealed?class?RandImage
????{
????????private?const?string?RandCharString?=?"0123456789";
????????private?int?width;
????????private?int?height;
????????private?int?length;
????????///?<summary>
????????///?默認構造函數(shù),生成的圖片寬度為48×24,隨即字符串字符個數(shù)
????????///?</summary>
????????public?RandImage():this(48,24,4)
????????{
????????}
????????///?<summary>
????????///?指定生成圖片的寬和高,默認生成圖片的字符串長度為4個字符
????????///?</summary>
????????///?<param?name="width"></param>
????????///?<param?name="height"></param>
????????public?RandImage(int?width,?int?height):this(width,height,4)
????????{
????????}
????????///?<summary>
????????///?指定生成圖片的寬和高以及生成圖片的字符串字符個數(shù)
????????///?</summary>
????????///?<param?name="width"></param>
????????///?<param?name="height"></param>
????????///?<param?name="length"></param>
????????public?RandImage(int?width,?int?height,?int?length)
????????{
????????????this.width?=?width;
????????????this.height?=?height;
????????????this.length?=?length;
????????}
????????///?<summary>
????????///?以默認的大小和默認的字符個數(shù)產生圖片
????????///?</summary>
????????///?<returns></returns>
????????public?Image?GetImage()
????????{
????????????Bitmap?p_w_picpath?=?new?Bitmap(width,?height);
????????????Graphics?g?=?Graphics.FromImage(p_w_picpath);
????????????g.Clear(Color.White);
????????????string?randString?=?"";
????????????Random?random=new?Random();
????????????do
????????????{ //使用DateTime.Now.Millisecond作為生成隨機數(shù)的參數(shù),增加隨機性
????????????????randString?+=?RandCharString.Substring(random.Next(DateTime.Now.Millisecond)%RandCharString.Length,?1);
????????????}
????????????while?(randString.Length?<?4);
????????????float?emSize=(float)width/randString.Length;
????????????Font?font?=?new?Font("Arial",?emSize,?(System.Drawing.FontStyle.Bold?|?System.Drawing.FontStyle.Italic));
????????????Pen?pen?=?new?Pen(Color.Silver);
????????????#region?畫圖片的背景噪音線
????????????int?x1,y1,x2,y2;
????????????
????????????for?(int?i?=?0;?i?<?25;?i++)
????????????{
????????????????x1?=?random.Next(p_w_picpath.Width);
????????????????y1?=?random.Next(p_w_picpath.Height);
????????????????x2?=?random.Next(p_w_picpath.Width);
????????????????y2?=?random.Next(p_w_picpath.Height);
????????????????g.DrawLine(pen,?x1,?y1,?x2,?y2);
????????????}
????????????#endregion
????????????#region?畫圖片的前景噪音點
????????????for?(int?i?=?0;?i?<?100;?i++)
????????????{
????????????????x1?=?random.Next(p_w_picpath.Width);
????????????????y1?=?random.Next(p_w_picpath.Height);
????????????????p_w_picpath.SetPixel(x1,?y1,?Color.FromArgb(random.Next(Int32.MaxValue)));
????????????}
????????????#endregion
????????????g.DrawString(randString,?font,?Brushes.Red,?2,?2);
????????????g.Dispose();
????????????return?p_w_picpath;
????????????
????????}
????}
} 將圖片轉換成html資源的aspx頁面,這個頁面前臺沒有任何代碼,主要后臺(.cs)的功能。 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.IO;
using?System.Drawing;
using?System.Drawing.Imaging;
using?Sooyie.Common;
public?partial?class?CheckImage?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(!Page.IsPostBack)
????????{
????????????RandImage?randImage=new?RandImage();
????????????System.Drawing.Image?p_w_picpath?=?randImage.GetImage();
????????????System.IO.MemoryStream?memoryStream?=?new?MemoryStream();
????????????p_w_picpath.Save(memoryStream,?ImageFormat.Jpeg);
????????????Response.ClearContent();
????????????Response.ContentType?=?"p_w_picpath/gif";
????????????Response.BinaryWrite(memoryStream.ToArray());
????????????p_w_picpath.Dispose();
????????????Response.End();
????????}
????}
} 下面是怎么使用的例子: <%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="Login.aspx.cs"?Inherits="Admin_Login"?%>
<!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>
<body>
????<form?id="form1"?runat="server">
????<div>
????<table?border="0"?cellpadding="0"?cellspacing="0"?width="480">
????????<tr>
????????????<td?colspan="2"?align="center">
????????????????公文管理系統(tǒng)后臺登陸</td>
????????</tr>
????????<tr>
????????????<td?style="width:?83px">
????????????????用戶名</td><td>
????????????????<asp:TextBox?ID="txtUserName"?runat="server"></asp:TextBox>
????????????????????<asp:RequiredFieldValidator?ID="RequiredFieldValidator1"?runat="server"?ControlToValidate="txtUserName"
????????????????????????ErrorMessage="用戶名"></asp:RequiredFieldValidator></td>
????????</tr>
????????<tr>
????????????<td?style="height:?19px;?width:?83px;">
????????????????密碼</td><td>
????????????????<asp:TextBox?ID="txtPassword"?runat="server"?TextMode="Password"></asp:TextBox>
????????????????????<asp:RequiredFieldValidator?ID="RequiredFieldValidator2"?runat="server"?ControlToValidate="txtPassword"
????????????????????????ErrorMessage="密碼"></asp:RequiredFieldValidator></td>
????????</tr>
????????<tr>
????????????<td?style="height:?19px;?width:?83px;">
????????????????校驗碼</td><td>
????????????????<asp:TextBox?ID="txtCheckCode"?runat="server"></asp:TextBox><img?src="CheckImage.aspx"?alt="校驗碼"?/>
????????????????????<asp:RequiredFieldValidator?ID="RequiredFieldValidator3"?runat="server"?ControlToValidate="txtCheckCode"
????????????????????????ErrorMessage="校驗碼必填"></asp:RequiredFieldValidator></td>
????????</tr>
????????<tr>
????????????<td?style="width:?83px">
????????????????<asp:Button?ID="btnLogin"?runat="server"?Text="登陸"?OnClick="btnLogin_Click"?/></td><td?style="width:?9px">
????????????????????<input?id="Reset1"?type="reset"?value="清除"?/></td>
????????</tr>
????</table>
????</div>
????????<asp:Literal?ID="lStatus"?runat="server"?Visible="False"></asp:Literal>
????</form>
</body>
</html> 請注意:<img src="CheckImage.aspx" alt="校驗碼" />這一句就使用了CheckImage.aspx.cs類中的功能,將圖片通過HTTP輸出。
using?System.Collections.Generic;
using?System.Text;
using?System.Drawing;
namespace?Sooyie.Common
{
????///?<summary>
????///?產生隨即圖片
????///?</summary>
????public?sealed?class?RandImage
????{
????????private?const?string?RandCharString?=?"0123456789";
????????private?int?width;
????????private?int?height;
????????private?int?length;
????????///?<summary>
????????///?默認構造函數(shù),生成的圖片寬度為48×24,隨即字符串字符個數(shù)
????????///?</summary>
????????public?RandImage():this(48,24,4)
????????{
????????}
????????///?<summary>
????????///?指定生成圖片的寬和高,默認生成圖片的字符串長度為4個字符
????????///?</summary>
????????///?<param?name="width"></param>
????????///?<param?name="height"></param>
????????public?RandImage(int?width,?int?height):this(width,height,4)
????????{
????????}
????????///?<summary>
????????///?指定生成圖片的寬和高以及生成圖片的字符串字符個數(shù)
????????///?</summary>
????????///?<param?name="width"></param>
????????///?<param?name="height"></param>
????????///?<param?name="length"></param>
????????public?RandImage(int?width,?int?height,?int?length)
????????{
????????????this.width?=?width;
????????????this.height?=?height;
????????????this.length?=?length;
????????}
????????///?<summary>
????????///?以默認的大小和默認的字符個數(shù)產生圖片
????????///?</summary>
????????///?<returns></returns>
????????public?Image?GetImage()
????????{
????????????Bitmap?p_w_picpath?=?new?Bitmap(width,?height);
????????????Graphics?g?=?Graphics.FromImage(p_w_picpath);
????????????g.Clear(Color.White);
????????????string?randString?=?"";
????????????Random?random=new?Random();
????????????do
????????????{ //使用DateTime.Now.Millisecond作為生成隨機數(shù)的參數(shù),增加隨機性
????????????????randString?+=?RandCharString.Substring(random.Next(DateTime.Now.Millisecond)%RandCharString.Length,?1);
????????????}
????????????while?(randString.Length?<?4);
????????????float?emSize=(float)width/randString.Length;
????????????Font?font?=?new?Font("Arial",?emSize,?(System.Drawing.FontStyle.Bold?|?System.Drawing.FontStyle.Italic));
????????????Pen?pen?=?new?Pen(Color.Silver);
????????????#region?畫圖片的背景噪音線
????????????int?x1,y1,x2,y2;
????????????
????????????for?(int?i?=?0;?i?<?25;?i++)
????????????{
????????????????x1?=?random.Next(p_w_picpath.Width);
????????????????y1?=?random.Next(p_w_picpath.Height);
????????????????x2?=?random.Next(p_w_picpath.Width);
????????????????y2?=?random.Next(p_w_picpath.Height);
????????????????g.DrawLine(pen,?x1,?y1,?x2,?y2);
????????????}
????????????#endregion
????????????#region?畫圖片的前景噪音點
????????????for?(int?i?=?0;?i?<?100;?i++)
????????????{
????????????????x1?=?random.Next(p_w_picpath.Width);
????????????????y1?=?random.Next(p_w_picpath.Height);
????????????????p_w_picpath.SetPixel(x1,?y1,?Color.FromArgb(random.Next(Int32.MaxValue)));
????????????}
????????????#endregion
????????????g.DrawString(randString,?font,?Brushes.Red,?2,?2);
????????????g.Dispose();
????????????return?p_w_picpath;
????????????
????????}
????}
} 將圖片轉換成html資源的aspx頁面,這個頁面前臺沒有任何代碼,主要后臺(.cs)的功能。 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.IO;
using?System.Drawing;
using?System.Drawing.Imaging;
using?Sooyie.Common;
public?partial?class?CheckImage?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(!Page.IsPostBack)
????????{
????????????RandImage?randImage=new?RandImage();
????????????System.Drawing.Image?p_w_picpath?=?randImage.GetImage();
????????????System.IO.MemoryStream?memoryStream?=?new?MemoryStream();
????????????p_w_picpath.Save(memoryStream,?ImageFormat.Jpeg);
????????????Response.ClearContent();
????????????Response.ContentType?=?"p_w_picpath/gif";
????????????Response.BinaryWrite(memoryStream.ToArray());
????????????p_w_picpath.Dispose();
????????????Response.End();
????????}
????}
} 下面是怎么使用的例子: <%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="Login.aspx.cs"?Inherits="Admin_Login"?%>
<!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>
<body>
????<form?id="form1"?runat="server">
????<div>
????<table?border="0"?cellpadding="0"?cellspacing="0"?width="480">
????????<tr>
????????????<td?colspan="2"?align="center">
????????????????公文管理系統(tǒng)后臺登陸</td>
????????</tr>
????????<tr>
????????????<td?style="width:?83px">
????????????????用戶名</td><td>
????????????????<asp:TextBox?ID="txtUserName"?runat="server"></asp:TextBox>
????????????????????<asp:RequiredFieldValidator?ID="RequiredFieldValidator1"?runat="server"?ControlToValidate="txtUserName"
????????????????????????ErrorMessage="用戶名"></asp:RequiredFieldValidator></td>
????????</tr>
????????<tr>
????????????<td?style="height:?19px;?width:?83px;">
????????????????密碼</td><td>
????????????????<asp:TextBox?ID="txtPassword"?runat="server"?TextMode="Password"></asp:TextBox>
????????????????????<asp:RequiredFieldValidator?ID="RequiredFieldValidator2"?runat="server"?ControlToValidate="txtPassword"
????????????????????????ErrorMessage="密碼"></asp:RequiredFieldValidator></td>
????????</tr>
????????<tr>
????????????<td?style="height:?19px;?width:?83px;">
????????????????校驗碼</td><td>
????????????????<asp:TextBox?ID="txtCheckCode"?runat="server"></asp:TextBox><img?src="CheckImage.aspx"?alt="校驗碼"?/>
????????????????????<asp:RequiredFieldValidator?ID="RequiredFieldValidator3"?runat="server"?ControlToValidate="txtCheckCode"
????????????????????????ErrorMessage="校驗碼必填"></asp:RequiredFieldValidator></td>
????????</tr>
????????<tr>
????????????<td?style="width:?83px">
????????????????<asp:Button?ID="btnLogin"?runat="server"?Text="登陸"?OnClick="btnLogin_Click"?/></td><td?style="width:?9px">
????????????????????<input?id="Reset1"?type="reset"?value="清除"?/></td>
????????</tr>
????</table>
????</div>
????????<asp:Literal?ID="lStatus"?runat="server"?Visible="False"></asp:Literal>
????</form>
</body>
</html> 請注意:<img src="CheckImage.aspx" alt="校驗碼" />這一句就使用了CheckImage.aspx.cs類中的功能,將圖片通過HTTP輸出。
轉載于:https://blog.51cto.com/zhoufoxcn/166995
總結
以上是生活随笔為你收集整理的asp.net中验证码的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一次执行时没有问题,重复执行会出错、G
- 下一篇: 中国股市暴涨暴跌全记录