博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Asp.Net2.0下C#环境 Login控件实现用户登录

Posted on 2008-04-29 11:46  张明  阅读(14826)  评论(5编辑  收藏  举报
一、前台显示效果



二、前台代码
            <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"
                BorderWidth
="1px" Font-Names="Verdana" Font-Size="10pt" OnAuthenticate="Login1_Authenticate">
                
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                
<LayoutTemplate>
                    
<table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
                        
<tr>
                            
<td>
                                
<table border="0" cellpadding="0">
                                    
<tr>
                                        
<td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #6b696b">
                                            用户登录
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right">
                                            
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label></td>
                                        
<td>
                                            
<asp:TextBox ID="UserName" runat="server" CssClass="STYLE1"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                ErrorMessage
="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right">
                                            
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label></td>
                                        
<td>
                                            
<asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="STYLE1"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                                ErrorMessage
="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right">
                                            
<asp:Label ID="Label1" runat="server" AssociatedControlID="Password">验证码:</asp:Label></td>
                                        
<td>
                                            
<asp:TextBox ID="GetCode" runat="server"  CssClass="STYLE2"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Password"
                                                ErrorMessage
="必须填写“验证码”。" ToolTip="必须填写“验证码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator><asp:Image ID="Image1" runat="server" ImageUrl="~/include/GetValidate.aspx" ImageAlign="Top"
                                                                            alt
="看不清?点击更换" onclick="this.src=this.src+'?'" />
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="center" colspan="2" style="color: red">
                                            
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="center" colspan="2">
                                            
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="登录" ValidationGroup="Login1" /></td>
                                    
</tr>
                                
</table>
                            
</td>
                        
</tr>
                    
</table>
                
</LayoutTemplate>
            
</asp:Login>

三、后台代码

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    
{
        TextBox GetCode 
= Login1.FindControl("GetCode"as TextBox;//获取登陆控件中验证码文本框值
        if (Request.Cookies["CheckCode"].Value == null)
        
{
            Response.Write(
@"<script language=JavaScript>{window.alert('您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统!');}</script>");
            
return;
        }

        
else
        
{
            
if (String.Compare(Request.Cookies["CheckCode"].Value, GetCode.Text.ToString().Trim(), true!= 0)
            
{
                Response.Write(
@"<script language=JavaScript>{window.alert('验证码输入不正确!');}</script>");
                
return;
            }

            
string UserLoginID = Login1.UserName.ToString().Trim().Replace("'""").Replace("=""");//得到输入的用户名
            string UserLoginPwd = Login1.Password.ToString().Trim().Replace("'""").Replace("=""");//得到输入的密码
            
//得到md5值
            string md5Pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(UserLoginPwd, "md5").ToLower();
            
string mySql = "select * from [User] where [sLoginID]='" + UserLoginID + "' and [LoginPWD]='" + md5Pwd + "'";
            
//下面部署自己的逻辑处理,以下仅供参考
            DBConn myDB = new DBConn();
            
try
            
{
                SqlDataReader Rs 
= myDB.getDataReader(mySql);
                
if (!Rs.Read())
                
{
                    e.Authenticated 
= false;//登录不通过
                }

                
else
                
{
                    Session[
"UserLoginID"= UserLoginID;
                    Session[
"UserLoginPwd"= UserLoginPwd;
                    e.Authenticated 
= true;//登录通过
                    Response.Redirect("index.aspx");
                }

            }

            
catch (Exception ex)
            
{
                Response.Write(
"数据库错误,错误原因:" + ex.Message);
                Response.End();
            }


        }

    }

 四、关于验证码可以本Blog的随笔中找到,地址为:

http://www.cnblogs.com/zm235/archive/2006/10/02/520233.html