2013-03-25 99 views
0

我的登錄頁面控件代碼:編寫代碼一

<table class="auto-style9"> 
    <tr> 
     <td class="auto-style12" colspan="2" style="font-family: 
     Georgia; font-size: medium; font-weight: bold; 
     text-transform: uppercase; color: #000000">Login 
     </td> 
    </tr> 
    <tr> 
     <td class="auto-style15">User name</td> 
     <td class="auto-style15"> 
     <asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox> 
     </td> 
    </tr> 
    <tr> 
     <td class="auto-style15">Password </td> 
     <td class="auto-style15"> 
     <asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password">    
     </asp:TextBox> 
     </td> 
    </tr> 
    <tr> 
     <td class="auto-style15">&nbsp;</td> 
     <td class="auto-style15"> 
     <asp:Button ID="ButtonLogin" runat="server" 
      CommandName="Login" Text="Login!" 
      OnClick="ButtonLogin_Click" BackColor="Black" 
      ForeColor="Yellow" /> 
     </td> 
    </tr> 
</table> 

我的按鈕登錄Click事件是:

protected void ButtonLogin_Click(object sender, EventArgs e) 
{ 
    using(BerouDataContext Data = new BerouDataContext()) 
    { 
     var UsernameCheck = UserNameTextBox.Text; 
     var PasswordCheck = PasswordTextBox.Text; 
     var UserExist = Data.Memberships.Single(s => s.Username == UsernameCheck); 
     if (UserExist == null || UserExist.Password != PasswordCheck) 
     { 
     LabelLoginValidity.Text = "Login Details are incorrect."; 
     } 
     else 
     { 
     LabelLoginValidity.Text = "Login Successfull!"; 
     } 
    } 
} 

我的問題是如何使餅乾,如何編寫代碼對於c#中的loginStatus,請使用一些代碼來實現,謝謝。

回答

0

所以基本上,你想確定用戶是否登錄。

,您可以利用的Session變量或Cookie可變

在其他部分的ButtonLogin_Click裏面,當你登錄成功, 添加這些行

else 
    { 
    LabelLoginValidity.Text = "Login Successfull!"; 
    Session["loggedIn"]=true; 
    //or you can create cookie like this 

     HttpCookie myCookie = new HttpCookie("myCookie"); 

     //Add key-values in the cookie 
     myCookie.Values.Add("userid", objUser.id.ToString()); 

     //set cookie expiry date-time. Made it to last for next 30 minutes. 
     myCookie.Expires = DateTime.Now.AddMinutes(30); 

     //Most important, write the cookie to client. 
     Response.Cookies.Add(myCookie); 
    } 

現在這個會話或cookie變量,你可以檢查內頁。像 您的主頁上

protected void Page_load(object sender, EventArgs e) 
{ 
     if(Session["loggedIn"]==null) 
     { 
      //Session doesn't exist, redirect the user to login page 
      Response.Redirect("Login.aspx"); 
     } 
} 

和你的內心必須銷燬在退出按鈕會話或cookie變量點擊即

protected void btnLogout_Click(object sender, EventArgs e) 
{ 
    Session.Abandon(); 
    //or 
    //Session.Remove("loggedIn"); 
} 

所以基本上,SessionCookie的狀態管理技術。

閱讀more about them here

+0

謝謝你的幫助,我會檢查它:) – VINNUSAURUS 2013-03-27 14:01:00

0

請不要重新發明輪子;改爲使用ASP.Net的FormsAuthentication

這比編寫自己的登錄邏輯更安全。

protected void ButtonLogin_Click(object sender, EventArgs e) 
{ 
    using(BerouDataContext Data = new BerouDataContext()) 
    { 
     var UsernameCheck = UserNameTextBox.Text; 
     var PasswordCheck = PasswordTextBox.Text; 
     var UserExist = Data.Memberships.Single(s => s.Username == UsernameCheck); 
     if (UserExist == null || UserExist.Password != PasswordCheck) 
     { 
     LabelLoginValidity.Text = "Login Details are incorrect."; 
     } 
     else 
     { 
     FormsAuthentication.SetAuthCookie(UserNameTextBox.Text, false); 
     LabelLoginValidity.Text = "Login Successfull!"; 
     } 
    } 
} 

這裏是logout

+0

謝謝你的幫助,我會檢查它:) – VINNUSAURUS 2013-03-27 14:00:32