留言板----一个登陆页面(带验证码)


<body>
    <form id="form1" runat="server">
    <div>
        <center>
            <table border="1" cellpadding="0" cellspacing="0">
                <tr>
                    <td colspan="2" style="color: #ffffff; background-color: #669900; height: 32px">
                        <span style="font-size: 24pt">
                            登陆
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>用 户 名: </td>
                    <td>
                    <asp:TextBox ID="unameTextBox" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        密&nbsp &nbsp &nbsp &nbsp 码:
                    </td>
                     <td>
                    <asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        图片验证:
                    </td>
                    <td align="left">
                         <asp:TextBox ID="ValidateTextBox" runat="server" Width="71px" Height="25px"></asp:TextBox>
                         <asp:ImageButton ID="ValidateImageButton" runat="server" />
                    </td> 
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="MsgLabel" runat="server"  ForeColor="red" Font-Size="small"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="LandButton" runat="server" Text="登陆" OnClick="LandButton_Click" />
                    </td>
                </tr>
            </table>
        </center>
    </div>
    </form>
</body>

后台代码:

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ValidateImageButton.ImageUrl = "image.aspx";
    }
    protected void LandButton_Click(object sender, EventArgs e)
    {
        if ((this.unameTextBox.Text == "") || (this.PasswordTextBox.Text==""))
        {
            MsgLabel.Text = "用户名与密码不能为空!";
        }
        else
        {
            SqlConnection con = db.CreateConnection();
            con.Open();
            string strsql = "select upass from login where uname='"+this.unameTextBox.Text+"'";
            SqlCommand cmd = new SqlCommand(strsql, con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(strsql, con);
            da.Fill(ds, "mytable");

            try
            {
                if (this.PasswordTextBox.Text == ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim())
                {
                    string curuser = this.unameTextBox.Text;
                    Session["uname"] = this.unameTextBox.Text.Trim();
                    string aa = this.ValidateTextBox.Text.ToString();
                    if (aa == Convert.ToString(Session["Image"])||aa==Convert.ToString(Session["Image"]).ToLower())
                    {
                        Response.Redirect("message.aspx");
                    }
                    else
                    {
                        Response.Write("<script>alert('验证码错误,请重新输入!')</script>");
                    }
                }
                else
                {
                    MsgLabel.Text = "用户名或者密码错误!";
                }
            }
            catch
            {
                MsgLabel.Text = "sorry!你输入的用户名不存在!";
            }
            con.Close();
        }
    }
}

验证码生产页面:
首先创建一个新的页面image.aspx,此页面只需要编写后台代码即可,代码如下:

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;

public partial class image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       //产生验证图片
        this.GenImg(this.GenCode(4));

        //将验证码存储到Session中,以便需要时进行验证
        Session["image"] = this.GenCode(4);
    }

    //任意产生4个验证码
    private string GenCode(int p_num)
    {
        string[] source ={ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",  "D",  "E",  "F",  "G", 
            "G", "H", "I", "G", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",  "V",  "W",  "X",  "Y",  "Z"};
        string code = "";
        Random rd = new Random();

        for (int i = 0; i < p_num; i++)
        {
            code += source[rd.Next(0, source.Length)];
        }

        return code;
    }

    //生产图片
    private void GenImg(string p_code)
    {
        //定义一个画板
        Bitmap myPalette = new Bitmap(60,20);

        //在画板上定义绘图的实例
        Graphics gh = Graphics.FromImage(myPalette);

        //定义一个矩形
        Rectangle rc = new Rectangle(0,0,60,20);

        //填充矩形
        gh.FillRectangle(new SolidBrush(Color.Blue), rc);

        //在矩形内画出字符串
        gh.DrawString(p_code, new Font("宋体", 16), new SolidBrush(Color.White), rc);

        //将图片显示出来
        myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        gh.Dispose();
        myPalette.Dispose();

    }
}



posted @ 2008-06-21 09:21  不染丹心  阅读(753)  评论(0编辑  收藏  举报