登录实现。。。娱乐一下吧。最近有人说加验证码的登录如何实现。。。。所以随手写了一个东东。懂的人就娱乐一下。不懂的。学习吧!

第一:先贴上界面吧!  布局就随便了。意思意思就行了。关键是后天逻辑上的问题 必要的是验证码的输入框和验证码。

<tbody>
                                                <tr>
                                                    <td colspan="2" height="50">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="right" width="35%" height="20">
                                                        <strong>用户名:</strong>
                                                    </td>
                                                    <td width="65%" height="20" align="left">
                                                        <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="right" height="20">
                                                        <strong>密 码:</strong>
                                                    </td>
                                                    <td height="20" align="left">
                                                        <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="right" height="20">
                                                        <strong>认证码:</strong>
                                                    </td>
                                                    <td height="20" align="left">
                                                        <asp:TextBox ID="txtYanzheng" runat="server" Width="80px"></asp:TextBox>
                                                        <img alt="刷新" onclick="this.src='Image.aspx?id=' + Math.random() * 10000" src="Image.aspx" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="right" height="10">
                                                        &nbsp;
                                                    </td>
                                                    <td height="10">
                                                    </td>
                                                </tr>
                                                <tr align="middle">
                                                    <td colspan="2" height="16">
                                                        <hr width="270" color="#0a4d99" noshade size="1">
                                                    </td>
                                                </tr>
                                                <tr valign="top" align="middle">
                                                    <td colspan="2" height="25">
                                                        <asp:ImageButton ID="btLogin" runat="server" ImageUrl="~/Admin/images/bgoffice_1.gif"
                                                            OnClick="btLogin_Click" />
                                                        &nbsp;&nbsp;
                                                        <asp:ImageButton ID="btCancel" runat="server" ImageUrl="~/Admin/images/bgoffice_2.gif"
                                                            OnClick="btCancel_Click" />
                                                    </td>
                                                </tr>
                                            </tbody>

 

第二部。后台CS逻辑也就是登录事件

   protected void btLogin_Click(object sender, ImageClickEventArgs e)
        {
            string code = txtYanzheng.Text.ToString();//变量保存用户输入的验证码。
            string UID = txtUser.Text.ToString();//用户名 不解释
            string PWD = txtPwd.Text.ToString();// 密码不解释
            if (Request.Cookies["CheckCode"] == null) //判断cookie不解释
            {
                JScript.Alert("您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。", Page);
                return;
            }
            else if (String.Compare(Request.Cookies["CheckCode"].Value, code, true) != 0)//关键部分。。判断验证码的值与输入时候相互用到一个 string.compare
            {
                JScript.Alert("验证码错误,请输入正确的验证码。", Page);//友好。提示
                return;
            }
            else
            {
                if (txtUser.Text != "" && txtPwd.Text != "")
                {
                    Vincent.BLL.Admin user = new Vincent.BLL.Admin();
                    if (user.Exists(txtUser.Text.ToString(), txtPwd.Text.ToString()))
                    {
                        Session["UID"] = txtUser.Text.ToString();
                        HttpCookie cookie = new HttpCookie("cookiename");
                        cookie.Value = this.txtUser.Text;
                        Response.AppendCookie(cookie);
                        Response.Redirect("Default.aspx"); ;
                    }
                    else
                        JScript.Alert("用户名或密码错误!", Page);
                }
                else
                    JScript.Alert("请输入用户名或密码!", Page);
            }
        }

 

第三:完结。关于验证码本博客 貌似有写过。如果不会,找一下吧。

posted @ 2012-05-29 17:16  黑桃_K  阅读(96)  评论(0编辑  收藏  举报