smhy8187

 

asp.net图片验证代码

asp.net图片验证代码
2006年07月28日 星期五 14:45

image.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.Drawing.Imaging;
using System.IO;
using System.Drawing;
public partial class image : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
          string tmp = RndNum(4);

          Session["yanzheng"] = tmp;
          this.ValidateCode(tmp);

 

     }
     private void ValidateCode(string VNum)
     {
         Bitmap Img = null;
         Graphics g = null;
         MemoryStream ms = null;

         int gheight = VNum.Length * 12;
         Img = new Bitmap(gheight, 25);
         g = Graphics.FromImage(Img);
         //背景颜色
         g.Clear(Color.White);
         //文字字体
         Font f = new Font("Arial Black", 10);
         //文字颜色
         SolidBrush s = new SolidBrush(Color.Black);
         g.DrawString(VNum, f, s, 3, 3);
         ms = new MemoryStream();
         Img.Save(ms, ImageFormat.Jpeg);
         Response.ClearContent();
         Response.ContentType = "image/Jpeg";
         Response.BinaryWrite(ms.ToArray());

         g.Dispose();
         Img.Dispose();
         Response.End();
     }

     private string RndNum(int VcodeNum)
     {
         string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
          ",q,r,s,t,u,v,w,x,y,z";
         string[] VcArray = Vchar.Split(new Char[] { ',' });
         string VNum = "";
         int temp = -1;

         Random rand = new Random();

         for (int i = 1; i < VcodeNum + 1; i++)
         {
             if (temp != -1)
             {
                 rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
             }

             int t = rand.Next(35);
             if (temp != -1 && temp == t)
             {
                 return RndNum(VcodeNum);
             }
             temp = t;
             VNum += VcArray[t];
         }
         return VNum;
     }


}

login.aspx:

 

<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
     <title>无标题页</title>
    
          <script language="JavaScript" type="text/javascript">
           <!--
                     function CheckForm(thisform)
                     {
                      if(thisform.T_name.value=="") {
                                     thisform.T_name.focus();
                                     thisform.T_name.select();
                                     alert("请输入用户名!");
                                     return false;                            
                             }         
                         if(thisform.T_pwd.value=="") {
                                     thisform.T_pwd.focus();
                                     thisform.T_pwd.select();
                                     alert("请输入密码!");
                                     return false;                            
                             }     
                         if(thisform.T_yz.value=="") {
                                     thisform.T_yz.focus();
                                     thisform.T_yz.select();
                                     alert("请输入验证图片!");
                                     return false;                            
                             }                        
                      }
                       --->
         </script>

</head>
<body onkeydown="if(event.keyCode==13){ event.keyCode=8; document.getElementById('Button1').click();}">
    <form action="login.aspx" runat="server"   method="post" name="thisform" id="thisform" onsubmit="return CheckForm(this);">
   <div align="center">
     <table width="50%" border="0" cellspacing="0">
       <tr>
         <td height="30" colspan="2"><div align="center">用户登陆</div></td>
       </tr>
       <tr>
         <td width="50%" align="right">帐号:</td>
         <td height="25" align="left"><label>
           <input type="text" name="T_name" />
         </label></td>
       </tr>
       <tr>
         <td align="right">密码:</td>
         <td height="25" align="left"><label>
           <input type="password" name="T_pwd" size="21" />
         </label></td>
       </tr>
          <tr>
            <td align="right">验证图片:</td>
         <td height="25" align="left"><label>
           <input name="T_yz" type="text" size="8" />
             <asp:ImageButton ID="ImageButton1" runat="server" /></label></td>
       </tr>
          <tr>
         <td height="25" colspan="2" align="center"><label>
             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="确 定" />&nbsp;</label></td>
       </tr>
       </table>
   </div>
</form>
</body>
</html>

 

login.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.Data.SqlClient;
public partial class login : System.Web.UI.Page
{
     string str_yzma, T_yz, T_name, T_pwd, S_tr;
     SqlConnection Scon;
     SqlCommand Scmd;
   
     protected void Page_Load(object sender, EventArgs e)

     {
         ImageButton1.ImageUrl = "image.aspx";

         if (IsPostBack)
         {
             str_yzma = Session["yanzheng"].ToString();
             T_yz = Request.Form.Get("T_yz").ToString();
             T_name = Request.Form.Get("T_name").ToString();
             T_pwd = Request.Form.Get("T_pwd").ToString();
         }

 

     }

     protected void Button1_Click1(object sender, EventArgs e)
     {
         //str_yzma = Session["yanzheng"].ToString();
         //T_yz = Request.Form.Get("T_yz").ToString();
         //T_name = Request.Form.Get("T_name").ToString();
         //T_pwd = Request.Form.Get("T_pwd").ToString();

         if (str_yzma != T_yz)
         {
             Response.Write("<script>alert('验证码错误!');</script>");
         }
         else
         {

             S_tr = "select * from Fzfs_user where U_name = '" + T_name + "' and U_pwd ='" + T_pwd + "'";
             Scon = new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
             Scmd = new SqlCommand(S_tr, Scon);
             Scmd.Connection.Open();
             SqlDataReader Rd = Scmd.ExecuteReader();

             if (Rd.Read())
             {
                 Response.Write("<script> alert('恭喜~!你登录成功')</script>");
                 Rd.Close();
                 Scon.Close();
             }
             else
             {
                 Response.Write("<script> alert('帐号/密码错误')</script>");
             }
         }
     }
}

 

config 自己修改

posted on 2007-08-01 07:28  new2008  阅读(272)  评论(0编辑  收藏  举报

导航