现在的位置: 首页 > 综合 > 正文

添加登陆界面,增加验证码功能。

2014年01月07日 ⁄ 综合 ⁄ 共 2986字 ⁄ 字号 评论关闭

 
index前台:
 
<div>
 <table style="width: 100%;">
 <tr>
 <td>
 用户名:</td>
 <td>
 &nbsp;
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 </td>
 </tr>
 <tr>
 <td>
 &nbsp;
 密码:</td>
 <td>
 &nbsp;
 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
 </td>
 </tr>
 <tr>
 <td>
 验证码:
 </td>
 <td>
 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
 
<img id="yzm" src="pic.aspx" Xonclick="this.src='pic.aspx?aaa='+new Date()" />
 </td>
 </tr>
 
<tr>
 <td>
 &nbsp;
 <asp:Button ID="btnLog" runat="server" Xonclick="btnLog_Click" Text="登录" />
 </td>
 <td>
 &nbsp;
 <asp:Button ID="btnCancle" runat="server" Text="取消" />
 </td>
 </tr>
 </table>
 </div>
 
index后台:
 

protected void btnLog_Click(object sender, EventArgs e)
 {
 if (TextBox1.Text.Trim() == "admin" && TextBox2.Text.Trim() == "888888" && Session["YZM"].ToString() == TextBox3.Text.Trim())
 {
 Response.Write("登录成功!!");
 }
 else
 {
 Response.Write("登录失败!!");
 }
 }
 
验证码后台:
 
protected void Page_Load(object sender, EventArgs e)
 {
 System.Drawing.Image img = new Bitmap(190,50);
 Graphics g = Graphics.FromImage(img);
 this.AddPoint(img,100);
 string code = this.GeneralCode();
 Session["YZM"] = code;
 Font font1 = new Font("宋体",40,FontStyle.Italic);
 g.DrawString(code, font1, Brushes.Red,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.White);
 }
 }
 //生成随机的文字
 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));
 }
 return sb.ToString();
 }
 
注册页后台
 
public partial class zhuce : System.Web.UI.Page, IRequiresSessionState //注意要继承这个类
 {
 protected void Page_Load(object sender, EventArgs e)
 {
 
}
 }
 
一般处理程序后台
 
public class YZM : 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 false;
 }
 }
 }
 

 

抱歉!评论已关闭.