Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C编写LoginStatus和LogoutStatus的代码#_C#_Asp.net_Session_Login_Loginstatus - Fatal编程技术网

C# 使用C编写LoginStatus和LogoutStatus的代码#

C# 使用C编写LoginStatus和LogoutStatus的代码#,c#,asp.net,session,login,loginstatus,C#,Asp.net,Session,Login,Loginstatus,我的登录页面控件代码为: <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

我的登录页面控件代码为:

<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>

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

因此,基本上,您需要确定用户是否登录

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

在您的
按钮登录\u单击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);
  }
现在,通过这个会话或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");
}
所以基本上,
Session
Cookie
都是状态管理技术


阅读

请不要重新发明轮子;而是使用ASP.Net的

它比编写自己的登录逻辑更安全

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

这是地址。

谢谢你的帮助我会查一下:)谢谢你的帮助我会查一下:)
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!";
      }
   }
}