无为清净楼资源网 Design By www.qnjia.com
验证码技术是为了防止暴力破解等而设定的。现在一般的网站注册等都提供验证码功能,特别是腾讯更是长长的一串。文中参考了别人的代码。有了就没有必要再写了。可以读一下。不过我测试时发现了两次PageLoad的问题。注释了两句即可。同时修改了namespaces。同时提供完整的验证说明:
1 新建VerifyCode.aspx
cs文件代码如下:
复制代码 代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
/**///// <summary>
/// 页面验证码程序
/// 使用:在页面中加入HTML代码 <img src="/UploadFiles/2021-04-02/VerifyCode.aspx">/// </summary>
public partial class VerifyCode : System.Web.UI.Page
...{
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;
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
//InitializeComponent();
//base.OnInit(e);
}
/**//**//**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
...{
//this.Load += new System.EventHandler(this.Page_Load);
}
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Page_Load(object sender, System.EventArgs e)
...{
if (!IsPostBack)
...{
//
// 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();
}
}
/**//**//**//// <summary>
/// 设置页面不被缓存
/// </summary>
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");
}
/**//**//**//// <summary>
/// 取得一个 4 位的随机码
/// </summary>
/// <returns></returns>
private string GetRandomCode()
...{
return Guid.NewGuid().ToString().Substring(0, 4);
}
/**//**//**//// <summary>
/// 随机取一个字体
/// </summary>
/// <returns></returns>
private Font GetFont()
...{
int fontIndex = _random.Next(0, FontItems.Length);
FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
return new Font(FontItems[fontIndex], 12, fontStyle);
}
/**//**//**//// <summary>
/// 取一个字体的样式
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private FontStyle GetFontStyle(int index)
...{
switch (index)
...{
case 0:
return FontStyle.Bold;
case 1:
return FontStyle.Italic;
default:
return FontStyle.Regular;
}
}
/**//**//**//// <summary>
/// 随机取一个笔刷
/// </summary>
/// <returns></returns>
private Brush GetBrush()
...{
int brushIndex = _random.Next(0, BrushItems.Length);
_brushNameIndex = brushIndex;
return BrushItems[brushIndex];
}
/**//**//**//// <summary>
/// 绘画事件
/// </summary>
private void OnPaint()
...{
Bitmap objBitmap = null;
Graphics g = null;
try
...{
objBitmap = new Bitmap(Width, Height);
g = Graphics.FromImage(objBitmap);
Paint_Background(g);
Paint_Text(g);
Paint_TextStain(objBitmap);
Paint_Border(g);
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
Response.ContentType = "image/gif";
}
catch ...{}
finally
...{
if (null != objBitmap)
objBitmap.Dispose();
if (null != g)
g.Dispose();
}
}
/**//**//**//// <summary>
/// 绘画背景颜色
/// </summary>
/// <param name="g"></param>
private void Paint_Background(Graphics g)
...{
g.Clear(BackColor);
}
/**//**//**//// <summary>
/// 绘画边框
/// </summary>
/// <param name="g"></param>
private void Paint_Border(Graphics g)
...{
g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
}
/**//**//**//// <summary>
/// 绘画文字
/// </summary>
/// <param name="g"></param>
private void Paint_Text(Graphics g)
...{
g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
}
/**//**//**//// <summary>
/// 绘画文字噪音点
/// </summary>
/// <param name="g"></param>
private void Paint_TextStain(Bitmap b)
...{
for (int n=0; n<30; n++)
...{
int x = _random.Next(Width);
int y = _random.Next(Height);
b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
}
}
}
2 页面引用:
<asp:Image ID="getcode" src="/UploadFiles/2021-04-02/VerifyCode.aspx">一般需要同时提供刷新功能(看不清楚换一张),代码如下
<asp:Image ID="getcode" src="VerifyCode.aspx">如使用了母版页,则用如下代码:
<img ID="getcode" alt="" src="VerifyCode.aspx"> <A href="javascript:getimgcode()">刷新验证码</A>
超链接对应的javascript如下:
<script language="javascript" type="text/javascript">
function getimgcode()
{
var getimagecode = document.getElementById("getcode");
getimagecode.src = "VerifyCode.aspx";
}
</script>
3 判断验证
上述代码是将验证码存贮在Session中,用code来标志。读取代码Session["code"].ToString();
使用中,我们只需要比较Session["code"].ToString()和文本框输入的串(TextBoxCode.Text)是否相同即可进行判断。
if(Session["code"].ToString().Trim().Equals(TextBoxCode.Text.Trim()))
...{
Response.Write("Success");
}
测试通过!
1 新建VerifyCode.aspx
cs文件代码如下:
复制代码 代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
/**///// <summary>
/// 页面验证码程序
/// 使用:在页面中加入HTML代码 <img src="/UploadFiles/2021-04-02/VerifyCode.aspx">/// </summary>
public partial class VerifyCode : System.Web.UI.Page
...{
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;
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
//InitializeComponent();
//base.OnInit(e);
}
/**//**//**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
...{
//this.Load += new System.EventHandler(this.Page_Load);
}
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Page_Load(object sender, System.EventArgs e)
...{
if (!IsPostBack)
...{
//
// 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();
}
}
/**//**//**//// <summary>
/// 设置页面不被缓存
/// </summary>
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");
}
/**//**//**//// <summary>
/// 取得一个 4 位的随机码
/// </summary>
/// <returns></returns>
private string GetRandomCode()
...{
return Guid.NewGuid().ToString().Substring(0, 4);
}
/**//**//**//// <summary>
/// 随机取一个字体
/// </summary>
/// <returns></returns>
private Font GetFont()
...{
int fontIndex = _random.Next(0, FontItems.Length);
FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
return new Font(FontItems[fontIndex], 12, fontStyle);
}
/**//**//**//// <summary>
/// 取一个字体的样式
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private FontStyle GetFontStyle(int index)
...{
switch (index)
...{
case 0:
return FontStyle.Bold;
case 1:
return FontStyle.Italic;
default:
return FontStyle.Regular;
}
}
/**//**//**//// <summary>
/// 随机取一个笔刷
/// </summary>
/// <returns></returns>
private Brush GetBrush()
...{
int brushIndex = _random.Next(0, BrushItems.Length);
_brushNameIndex = brushIndex;
return BrushItems[brushIndex];
}
/**//**//**//// <summary>
/// 绘画事件
/// </summary>
private void OnPaint()
...{
Bitmap objBitmap = null;
Graphics g = null;
try
...{
objBitmap = new Bitmap(Width, Height);
g = Graphics.FromImage(objBitmap);
Paint_Background(g);
Paint_Text(g);
Paint_TextStain(objBitmap);
Paint_Border(g);
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
Response.ContentType = "image/gif";
}
catch ...{}
finally
...{
if (null != objBitmap)
objBitmap.Dispose();
if (null != g)
g.Dispose();
}
}
/**//**//**//// <summary>
/// 绘画背景颜色
/// </summary>
/// <param name="g"></param>
private void Paint_Background(Graphics g)
...{
g.Clear(BackColor);
}
/**//**//**//// <summary>
/// 绘画边框
/// </summary>
/// <param name="g"></param>
private void Paint_Border(Graphics g)
...{
g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
}
/**//**//**//// <summary>
/// 绘画文字
/// </summary>
/// <param name="g"></param>
private void Paint_Text(Graphics g)
...{
g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
}
/**//**//**//// <summary>
/// 绘画文字噪音点
/// </summary>
/// <param name="g"></param>
private void Paint_TextStain(Bitmap b)
...{
for (int n=0; n<30; n++)
...{
int x = _random.Next(Width);
int y = _random.Next(Height);
b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
}
}
}
2 页面引用:
<asp:Image ID="getcode" src="/UploadFiles/2021-04-02/VerifyCode.aspx">一般需要同时提供刷新功能(看不清楚换一张),代码如下
<asp:Image ID="getcode" src="VerifyCode.aspx">如使用了母版页,则用如下代码:
<img ID="getcode" alt="" src="VerifyCode.aspx"> <A href="javascript:getimgcode()">刷新验证码</A>
超链接对应的javascript如下:
<script language="javascript" type="text/javascript">
function getimgcode()
{
var getimagecode = document.getElementById("getcode");
getimagecode.src = "VerifyCode.aspx";
}
</script>
3 判断验证
上述代码是将验证码存贮在Session中,用code来标志。读取代码Session["code"].ToString();
使用中,我们只需要比较Session["code"].ToString()和文本框输入的串(TextBoxCode.Text)是否相同即可进行判断。
if(Session["code"].ToString().Trim().Equals(TextBoxCode.Text.Trim()))
...{
Response.Write("Success");
}
测试通过!
标签:
asp.net,验证码
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月17日
2024年11月17日
- 中国武警男声合唱团《辉煌之声1天路》[DTS-WAV分轨]
- 紫薇《旧曲新韵》[320K/MP3][175.29MB]
- 紫薇《旧曲新韵》[FLAC/分轨][550.18MB]
- 周深《反深代词》[先听版][320K/MP3][72.71MB]
- 李佳薇.2024-会发光的【黑籁音乐】【FLAC分轨】
- 后弦.2012-很有爱【天浩盛世】【WAV+CUE】
- 林俊吉.2012-将你惜命命【美华】【WAV+CUE】
- 晓雅《分享》DTS-WAV
- 黑鸭子2008-飞歌[首版][WAV+CUE]
- 黄乙玲1989-水泼落地难收回[日本天龙版][WAV+CUE]
- 周深《反深代词》[先听版][FLAC/分轨][310.97MB]
- 姜育恒1984《什么时候·串起又散落》台湾复刻版[WAV+CUE][1G]
- 那英《如今》引进版[WAV+CUE][1G]
- 蔡幸娟.1991-真的让我爱你吗【飞碟】【WAV+CUE】
- 群星.2024-好团圆电视剧原声带【TME】【FLAC分轨】