使用c#编写LoginStatus和LogoutStatus代码



我的Login Page控件代码为:

<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编码,请帮助实现一些代码,谢谢。

所以基本上,你想确定,用户是否登录了

您可以使用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");
       }
 }

,你必须销毁会话或Cookie变量注销按钮点击即

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

所以基本上,SessionCookie是状态管理技术。

点击这里了解更多信息

请不要重新发明轮子;而是使用ASP。净的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!";
      }
   }
}

登出

相关内容

  • 没有找到相关文章

最新更新