添加登陆界面,增加验证码功能。
2012-12-26 20:35
417 查看
------------index.aspx--------------------
<body>
<form id="form1" runat="server">
<div>
<table style="width:40%; height: 124px;">
<tr>
<td>用户名:</td>
<td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>密 码:</td>
<td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>数字验证码:</td>
<td>
<asp:TextBox ID="txtYanZheng1" runat="server" Width="61px"
Height="19px"></asp:TextBox><asp:Image ID="Image1" runat="server" ImageUrl="~/pic.aspx"
onclick="this.src='pic.aspx?yzm='+new Date()"/></td>
</tr>
<tr>
<td> 字母验证码:</td>
<td>
<asp:TextBox ID="txtYanZheng2" runat="server" Width="61px"
Height="19px"></asp:TextBox> <img alt="" src="HandlerPic.ashx"
onclick="this.src='HandlerPic.ashx?yzm='+new Date()" />
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="登陆"
onclick="btnLogin_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
------------index.aspx.cs--------------------------
public partial class index : System.Web.UI.Page,IRequiresSessionState
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text != "admin")
{
Response.Write("用户名不正确!");
}
else if(txtPassword.Text != "111111")
{
Response.Write("密码不正确!");
}
else if (txtYanZheng1.Text!= Session["YZM"].ToString())
{
Response.Write("数字验证码不正确!");
}
else if (txtYanZheng2.Text != Session["CheckCode"].ToString())
{
Response.Write("字母验证码不正确!");
}
else
{
Response.Write("登陆成功!");
}
}
}
--------------pic.aspx.cs--------------------------
protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Image img = new Bitmap(150,60);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(new SolidBrush(Color.SkyBlue), 0, 0, 200, 60);
this.AddPoint(img, 200);
string code = this.GeneralCode();
Font font1 = new Font("宋体", 30, FontStyle.Italic);
g.DrawString(code, font1, Brushes.Tomato,0,0);
this.Response.Clear();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
this.Response.BinaryWrite(ms.ToArray());
this.Response.Flush();
this.Response.End();
}
private void AddPoint(System.Drawing.Image img, int nums)
{
Bitmap b = img as Bitmap;
Random ran = new Random();
for (int i = 0; i < nums; i++)
{
b.SetPixel(ran.Next(0, img.Width), ran.Next(0, img.Height), Color.Tomato);
}
}
private string GeneralCode()
{
Random ran = new Random(DateTime.Now.Millisecond);
StringBuilder sb = new StringBuilder(6);
for (int i = 0; i < 6; i++)
{
sb.Append(ran.Next(0, 9));
}
Session["YZM"] = sb.ToString();
return sb.ToString();
}
---------------HandlerPic.ashx--------------------------------
public class HandlerPic : IHttpHandler, IRequiresSessionState //
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
//建立Bitmap对象,绘图
Bitmap basemap = new Bitmap(150, 60);
Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.SkyBlue), 0, 0, 160, 60);
Font font = new Font(FontFamily.GenericSerif, 30, FontStyle.Bold,
GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder sb = new StringBuilder();
//添加随机的五个字母
for (int x = 0; x < 5; x++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
sb.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Tomato), x * 20, r.Next(0,
15));
}
//混淆背景
Pen linePen = new Pen(new SolidBrush(Color.Tomato), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 170), r.Next(0,40)), new Point
(r.Next(0, 170), r.Next(0, 40)));
//将图片保存到输出流中
basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["CheckCode"] = sb.ToString(); //如果没有实现
IRequiresSessionState,则这里会出错,也无法生成图片
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
<body>
<form id="form1" runat="server">
<div>
<table style="width:40%; height: 124px;">
<tr>
<td>用户名:</td>
<td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>密 码:</td>
<td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>数字验证码:</td>
<td>
<asp:TextBox ID="txtYanZheng1" runat="server" Width="61px"
Height="19px"></asp:TextBox><asp:Image ID="Image1" runat="server" ImageUrl="~/pic.aspx"
onclick="this.src='pic.aspx?yzm='+new Date()"/></td>
</tr>
<tr>
<td> 字母验证码:</td>
<td>
<asp:TextBox ID="txtYanZheng2" runat="server" Width="61px"
Height="19px"></asp:TextBox> <img alt="" src="HandlerPic.ashx"
onclick="this.src='HandlerPic.ashx?yzm='+new Date()" />
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="登陆"
onclick="btnLogin_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
------------index.aspx.cs--------------------------
public partial class index : System.Web.UI.Page,IRequiresSessionState
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text != "admin")
{
Response.Write("用户名不正确!");
}
else if(txtPassword.Text != "111111")
{
Response.Write("密码不正确!");
}
else if (txtYanZheng1.Text!= Session["YZM"].ToString())
{
Response.Write("数字验证码不正确!");
}
else if (txtYanZheng2.Text != Session["CheckCode"].ToString())
{
Response.Write("字母验证码不正确!");
}
else
{
Response.Write("登陆成功!");
}
}
}
--------------pic.aspx.cs--------------------------
protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Image img = new Bitmap(150,60);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(new SolidBrush(Color.SkyBlue), 0, 0, 200, 60);
this.AddPoint(img, 200);
string code = this.GeneralCode();
Font font1 = new Font("宋体", 30, FontStyle.Italic);
g.DrawString(code, font1, Brushes.Tomato,0,0);
this.Response.Clear();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
this.Response.BinaryWrite(ms.ToArray());
this.Response.Flush();
this.Response.End();
}
private void AddPoint(System.Drawing.Image img, int nums)
{
Bitmap b = img as Bitmap;
Random ran = new Random();
for (int i = 0; i < nums; i++)
{
b.SetPixel(ran.Next(0, img.Width), ran.Next(0, img.Height), Color.Tomato);
}
}
private string GeneralCode()
{
Random ran = new Random(DateTime.Now.Millisecond);
StringBuilder sb = new StringBuilder(6);
for (int i = 0; i < 6; i++)
{
sb.Append(ran.Next(0, 9));
}
Session["YZM"] = sb.ToString();
return sb.ToString();
}
---------------HandlerPic.ashx--------------------------------
public class HandlerPic : IHttpHandler, IRequiresSessionState //
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
//建立Bitmap对象,绘图
Bitmap basemap = new Bitmap(150, 60);
Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.SkyBlue), 0, 0, 160, 60);
Font font = new Font(FontFamily.GenericSerif, 30, FontStyle.Bold,
GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder sb = new StringBuilder();
//添加随机的五个字母
for (int x = 0; x < 5; x++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
sb.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Tomato), x * 20, r.Next(0,
15));
}
//混淆背景
Pen linePen = new Pen(new SolidBrush(Color.Tomato), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 170), r.Next(0,40)), new Point
(r.Next(0, 170), r.Next(0, 40)));
//将图片保存到输出流中
basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["CheckCode"] = sb.ToString(); //如果没有实现
IRequiresSessionState,则这里会出错,也无法生成图片
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
相关文章推荐
- 添加登陆界面,增加验证码功能。
- 温故知新--添加登陆界面,增加验证码功能
- Exchange邮箱登陆界面添加验证码功能完美解决方案 推荐
- JEECG--去掉(增加)登陆页面验证码功能
- Yii2框架实现登陆添加验证码功能示例
- jsp登陆验证,网页登陆验证带验证码校验,登录功能之添加验证码
- 如何给Domino登陆界面增加验证码
- 仿新浪微博2014之登陆界面四(总体功能)
- samba通过字符界面设置过程(字符界面添加samba的windows的登陆用户名) xK)ib!BdmJYr
- 【Android架构设置篇】之设置界面添加并实现永不休眠功能
- SSO单点登录系列5:cas单点登录增加验证码功能完整步骤
- 连连看提示功能的增加(添加了代码)
- 十九. 增加一个项目协作留言板功能(五)----- 增加添加附件功能
- 百度UEditor添加视频 增加支持“通用代码”功能,支持微信
- N0 通话界面增加录音计时功能
- FckEditor添加右键菜单;增加编辑区右键图片删除功能(asp.net )
- JS功能来实现登陆验证码
- 验证码类[置顶] SSO单点登录系列5:cas单点登录增加验证码功能完整步骤
- 飞鱼开发框架(一) 给软件登陆窗口增加记住密码和自动登陆功能(图文)
- 代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能