国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

淺析ASp.Net自定義驗證碼控件_.Net教程

2024-07-10 12:51:24
字體:
來源:轉載
供稿:網友

推薦:解讀ASP.NET網站程序防SQL注入式攻擊方法
一、什么是SQL注入式攻擊?所謂SQL注入式攻擊,就是攻擊者把SQL命令插入到Web表單的輸入域或頁面請求的查詢字符串,欺騙服務器執行惡意的SQL命令。在某些表單中,用戶輸入的內容直接用來

最近自己寫了一個自定義驗證碼控件把它拿出來和大家分享分享

具體步驟

1---》新建asp.net 網站

2---》添加新建項目 ,選擇類庫

3---》新建兩個類

3.1--》自定義控件類(WebControl 派生類)

以下為引用的內容:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AuthCode
{
[ToolboxData("〈{0}:AuthCode runat=server>〈/{0}:AuthCode>")]
public class AuthCode : WebControl
{
/// 〈summary>
/// 獲得驗證碼的值
/// 〈/summary>
/// 〈returns>驗證碼〈/returns>
public string GetValue()
{
return HttpContext.Current.Session["value"].ToString();
}
[Bindable(true)]
[Category("Appearance")]
[Description("驗證碼字符長度")]
[DefaultValue("ss")]
[Localizable(true)]
//長度
internal static int mySize;

public int MySize
{
get { return AuthCode.mySize; }
set
{
AuthCode.mySize = value;

}
}
 

public AuthCode()
: base(HtmlTextWriterTag.Img)//重寫父類的構造(輸出流的HTML標記)
{ }
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);//將要輸出的的HTML標簽的屬性和樣式添加到指定的 HtmlTextWriter中
writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");//添加樣式

/**-
* 圖片的onclick事件 "this.src='VerifyImg.jd?id=' Math.random()"
* 每次單擊一次就有一個新的圖片請求路徑(VerifyImg.jd?id=' Math.random())參數只是
* 告訴瀏覽器這是一個新的請求然后經過 IHttpHander處理生成新的圖片 id 沒有任何實際意思(創造一個新的請求)
* -**/
writer.AddAttribute("onclick", "this.src='img.jd?id=' Math.random()");//添加js VerifyImg.jd


writer.AddAttribute(HtmlTextWriterAttribute.Src, "img.jd");
writer.AddAttribute("alt", "點擊刷新");
}

}
}
 

3.2--》新建處理類(必須實現 IHttpHandler,IRequiresSessionState 兩個接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.SessionState;
using System.Drawing;
using System.IO;


namespace AuthCode
{
public class AuthCodeHttpHander:IHttpHandler,IRequiresSessionState
{
/// 〈summary>
/// 返回驗證碼字符
/// 〈/summary>
/// 〈param name="codeCount">驗證碼長度〈/param>
/// 〈returns>〈/returns>
private string GetRandomNumberString(int codeCount)
{
string strChoice = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strResult = strChoice.Split(new Char[] { ',' });
string strReturn = "";
Random rnd = new Random();
for (int i = 0; i 〈 codeCount; i )
{
int j = rnd.Next(strResult.Length);//隨機數不能大于數組的長度
strReturn = strReturn strResult[j].ToString();
}
return strReturn;
}

private Color GetColor()
{
return Color.Black;
}
private Bitmap CreateImage(string str_AuthCode)
{
/* -----------------------------繪制圖片的樣式 ------------------------------------*/

int width =str_AuthCode.Length* 21;
int height = 30;
Random rad = new Random();
Bitmap bmp = new Bitmap(width, height);
Graphics grp = Graphics.FromImage(bmp);// 在圖片上繪制圖形
grp.Clear(Color.YellowGreen);//填充bmp的背景色
grp.DrawRectangle(new Pen(Color.Red, 1), 0, 0, width - 1, height - 1);//繪制邊框
int num = width * height;
for (int i = 0; i 〈 num; i )//在圖片的指定坐標上畫上有顏色的圓點
{
int x = rad.Next(width);
int y = rad.Next(height);
int r = rad.Next(255);
int g = rad.Next(255);
int b = rad.Next(255);
Color c = Color.FromArgb(r, g, b);
bmp.SetPixel(x, y, c);//在圖片的指定坐標上畫上有顏色的圓點
}

/*-------------------------- 在圖片繪制字符串------------------------------------ */

Font f = new Font("宋體", 20, FontStyle.Bold);//定義字體
Brush br = new SolidBrush(Color.Black);//定義畫筆的顏色 及字體的顏色
for (int i = 0; i 〈 str_AuthCode.Length; i )
{
string s = str_AuthCode.Substring(i, 1);//單個單個的將字畫到圖片上
Point p = new Point(i * 20 rad.Next(3), rad.Next(3) 1);//字體出現的位置(坐標)
grp.DrawString(s, f, br, p);//繪制字符串
}
grp.Dispose();
return bmp;//返回

}


/// 〈summary>
/// 是否可以處理遠程的HTTP請求
/// 〈/summary>
public bool IsReusable
{
get { return true; }
}

/// 〈summary>
/// 將驗證碼圖片發送給WEB瀏覽器
/// 〈/summary>
/// 〈param name="context">〈/param>
public void ProcessRequest(HttpContext context)
{
int size = AuthCode.mySize; //Int32.Parse((String)context.Session["Size"]);
MemoryStream ms = new MemoryStream(); // 創建內存流(初始長度為0 自動擴充)
string NumStr = GetRandomNumberString(size);// 獲得驗證碼字符
context.Session.Add("value", NumStr);//將驗證碼字符保存到session里面
Bitmap theBitmap = CreateImage(NumStr);// 獲得驗證碼圖片
theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//將位圖寫入內存流
context.Response.ClearContent(); //清除緩沖區里的所有內容輸出
context.Response.ContentType = "image/jpeg"; //需要輸出圖象信息 要修改HTTP頭
context.Response.BinaryWrite(ms.ToArray()); //將內存流寫入HTTP輸出流
theBitmap.Dispose(); //釋放資源
ms.Close();//釋放資源
ms.Dispose();//釋放資源
context.Response.End();
}


}
}

分享:解讀asp.net中的觀察者模式
在asp.net中實現觀察者模式?難道asp.net中的觀察者模式有什么特別么?嗯,基于Http協議的Application難免有些健忘,我是這樣實現的,不知道有沒有更好的辦法? 先談談需求吧,以免陷入

共2頁上一頁12下一頁
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 甘德县| 宁城县| 仲巴县| 邓州市| 迁安市| 百色市| 孙吴县| 梁平县| 南投市| 盘山县| 洱源县| 东平县| 洛浦县| 清徐县| 葫芦岛市| 津南区| 文山县| 崇义县| 阿鲁科尔沁旗| 仪征市| 朝阳区| 南丹县| 藁城市| 阿荣旗| 林西县| 峨山| 彰武县| 宁陵县| 宜昌市| 郑州市| 禹城市| 莱州市| 哈尔滨市| 松滋市| 隆安县| 柯坪县| 三原县| 霍州市| 垦利县| 平利县|