验证码示例
验证码示例CheckCode.aspx
CheckCode.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 System.Drawing.Drawing2D;
public partial class CheckCode : System.Web.UI.Page
{
private Bitmap validateimage;
private Graphics g;
protected void Page_Load(object sender, EventArgs e)
{
Response.BufferOutput = true; //特别注意
Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
Response.AppendHeader(" Pragma", "No-Cache"); //特别注意
string VNum = MakeValidateCode();
Session["VNum"] = VNum;//取得验证码,以便后来验证
ValidateCode(VNum);
}
public void ValidateCode(string VNum)
{
validateimage = new Bitmap(60, 20, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(240, 255, 255, 255), Color.FromArgb(240, 255, 255, 255)), 0, 0, 200, 200); //矩形框
g.DrawString(VNum, new Font("arial", 11), new SolidBrush(Color.Red), new PointF(6, 0));//字体/颜色
g.Save();
MemoryStream ms = new MemoryStream();
validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
Response.End();
}
string MakeValidateCode()
{
char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};//枚举数组
string num = "";
Random r = new Random();
for (int i = 0; i < 4; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}
}
--------------------编程问答-------------------- <table border="0" align="center" background="images/login.jpg" style="width:600px; height:450px; background-repeat:no-repeat; vertical-align:middle;">
<tr>
<td align="center">
<table width="300" border="1" cellspacing="0" cellpadding="0" class="LoginTable" >
<tr>
<td> 姓名:</td>
<td> <asp:TextBox ID="UserNameTB" runat="server" Width="150px"></asp:TextBox></td>
</tr>
<tr>
<td style="height: 28px"> 密码:</td>
<td style="height: 28px"> <asp:TextBox ID="PasswordTB" runat="server" TextMode="Password" Width="150px"></asp:TextBox></td>
</tr>
<tr>
<td style="height: 28px">验证码</td>
<td style="height: 28px" valign="middle"><asp:TextBox ID="TextBox2" runat="server" Width="75px" AutoCompleteType="Disabled"> </asp:TextBox><img src="CheckCode.aspx" alt="验证码"/></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="LoginBtn" CssClass="LoginBtn" runat="server" Text="登录" OnClick="LoginBtn_Click" />
<asp:Button ID="CancelBtn" CssClass="LoginBtn" runat="server" Text="取消" OnClick="CancelBtn_Click" /></td>
</tr>
</table> --------------------编程问答-------------------- 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 DB;
public partial class login : System.Web.UI.Page
{
private string name;
private string password;
private DataTable dt = new DataTable();
private DataAccess da = new DataAccess();
private string sql;
protected void Page_Load(object sender, EventArgs e)
{
if (FSqlKeyWord.SqlKey.CheckRequestQuery(this.Request))
{
this.Response.Write("系统检测到您试图攻击本服务器,您的操作已经被记录!");
this.Response.End();
}
//string[] strs = { TextBox2.Text, UserNameTB.Text, PasswordTB.Text };
//if (Page.IsPostBack && FSqlKeyWord.SqlKey.CkeckArray(strs))
//{
// this.Response.Write("系统检测到您试图攻击本服务器,您的操作已经被记录!");
// this.Response.End();
//}
//System.IO.StreamReader sr;
//string file_path = Server.MapPath("IP.txt");
//sr = System.IO.File.OpenText(file_path);
//string str;
//bool ok = false;
//string IP = Request.UserHostAddress;
//while (!sr.EndOfStream)
//{
// str = sr.ReadLine().Trim();
// if (str == IP)
// ok = true;
//}
//sr.Close();
//if (!ok)
//{
// Response.Redirect("Default.aspx");
//}
}
protected void LoginBtn_Click(object sender, EventArgs e)
{
name = UserNameTB.Text;
password = PasswordTB.Text;
string validateFromClient = TextBox2.Text;
string validateCode = Session["VNum"].ToString();
if (validateCode != validateFromClient)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "validateCode", "<script>alert('验证码错误!');</script>");
return;
}
sql = "select admin_id from admin where name='" + name + "' and password='" + password + "'";
dt = da.GetTable(sql);
if (dt.Rows.Count == 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "<script>alert('用户名或密码错误!');</script>");
return;
}
int id = int.Parse(dt.Rows[0]["admin_id"].ToString());
Session["admin_id"] = id;
Response.Redirect("Manage/index.htm");
}
protected void CancelBtn_Click(object sender, EventArgs e)
{
UserNameTB.Text = "";
PasswordTB.Text = "";
return;
}
}
--------------------编程问答-------------------- 嗯,谢谢分享,就是太长了点。。。 --------------------编程问答--------------------
系统检测到您试图攻击本服务器,您的操作已经被记录!
有空去验证下 --------------------编程问答-------------------- 感谢分享 --------------------编程问答--------------------
补充:.NET技术 , ASP.NET