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>

我的按钮登录点击事件是:

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!";
      }
   }
}

我的问题是如何制作cookie,如何在c#中为loginStatus编写代码,请帮助一些代码来实现,谢谢。

4

2 回答 2

0

所以基本上,您要确定用户是否已登录。

您可以使用Session变量或Cookie变量

在你ButtonLogin_Click的 else 部分中,当你成功登录时,添加这些行

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

现在这个 Session 或 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");
       }
 }

并且您必须在注销按钮单击时销毁 Session 或 Cookie 变量,即

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

所以基本上,Session并且Cookie是状态管理技术。

在此处阅读有关它们的更多信息

于 2013-03-25T20:13:01.230 回答
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!";
      }
   }
}

这是注销

于 2013-03-25T20:36:36.893 回答