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


<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();

    }
}



转载于:https://www.cnblogs.com/burandanxin/archive/2008/06/21/1227196.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该压缩包为 imgchk 文件夹 ,其中有三个文件:validatecode.asp,validatebody.fix,validatehead.fix   主要即是为生成 验证码 服务的。   将解压文件夹直接释放在需要在某页添加验证码的同目录下,(比如 addnew.asp 需要添加验证码,该文件在 cnbruce 文件夹下,则将 imgchk 文件夹同时释放到 cnbruce 文件夹 中)   2、添加显示验证码和输入框   在需要添加验证码的页面中添加如下代码: <input name='validatecode' type='text' size='5'> <img src='imgchk/validatecode.asp' align='absmiddle' border='0'>   3、记录接受页面处理   在步骤2中,一定会将记录提交到某个页面处理,那么在该处理页中添加如下代码: If Trim(Request.Form("validatecode"))=Empty Or Trim(Session("cnbruce.com_ValidateCode"))<>Trim(Request.Form("validatecode")) Then response.write("请注意正确输入验证码") response.end end if   验证码原理:   在生成验证码图片的时候,同时生成了一个Session,其值就是验证码图片中的数字值。   同时,提供输入框让用户输入,提交输入值后,与已有的Session值进行比较,根据判断结果做相应判断。   如果需要修改Session的name,只有两个地方改动:   a:您下载的imgchk文件夹中的validatecode.asp,其中涉及到cnbruce.com的该成您的;   b:就是刚才的步骤 3中Trim(Session("cnbruce.com_ValidateCode"))<>Trim(Request.Form("validatecode")) 中的cnbruce.com换成a所设定的。   另外===================================================   显示校验码的补丁(有些朋友显示不出验证码,这里看看)   用下面内容创建文件SP2.reg(用记事本就可以,保存时选“另存为”) Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Security] "BlockXBM"=dword:00000000   双击导入注册表中,然后重启机器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值