⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.aspx

📁 asp.net_Lesson5
💻 ASPX
字号:
<HTML>
  <HEAD>
  <title>Cookie应用举例</title>
  <script Language="C#" runat="server">
  void Page_Load()
  {
    if(IsPostBack == true)
    {
      try
      {
        Hashtable userTable = HashFile.ReadHashtableFromFile(Server.MapPath("user.txt"));
        if( (string)userTable[txtUserName.Text] != txtPassword.Text )
        {
          Label1.Text = "用户名/密码不正确,请重试.";
          return;
        }

        Random rand = new Random();
        HttpCookie cookie1 = new HttpCookie("user");
        HttpCookie cookie2 = new HttpCookie("id");
        cookie1.Value = txtUserName.Text;
        cookie1.Expires = DateTime.Now.Add(new TimeSpan(0,0,10,0));
        cookie2.Value = rand.Next().ToString();
        cookie2.Expires = DateTime.Now.Add(new TimeSpan(0,0,10,0));
        Response.Cookies.Add(cookie1);
        Response.Cookies.Add(cookie2);

        Hashtable loginUser = HashFile.ReadHashtableFromFile(Server.MapPath("login.txt"));
        if( loginUser[cookie1.Value] == null)
        {
          loginUser.Add(cookie1.Value, cookie2.Value);
        }
        else
        {
          loginUser[cookie1.Value] = cookie2.Value;
        }
        HashFile.WriteHashtableToFile(loginUser, Server.MapPath("login.txt"));
        
        Label1.Text = "欢迎" + txtUserName.Text;
        txtUserName.Visible = false;
        txtPassword.Visible = false;
        lblUserName.Visible = false;
        lblPassword.Visible = false;
        Button1.Visible = false;
        HyperLink1.Visible = true;
        return;
      }
      catch(Exception e)
      {
        Response.Write(e.ToString());
        return;
      }
    }
    else
    {
      Label1.Text = "输入您的用户名与密码";
    }
  }
  </script>
	</HEAD>
	<body>
		<h3>登录页面</h3>
		<hr>
		<form id="form1" runat="server" method="post">
			<asp:Label id="Label1" runat="server" /><br>
			<asp:Label id="lblUserName" Runat="server">用户:</asp:Label>
			<asp:TextBox id="txtUserName" runat="server" /><br>
			<asp:Label id="lblPassword" Runat="server">密码:</asp:Label>
			<asp:TextBox id="txtPassword" TextMode="Password" runat="server" /><br>
			<asp:HyperLink id="HyperLink1" runat="server" NavigateUrl="Service.aspx" Visible="False">进入服务页面</asp:HyperLink>
			<P></P>
			<hr>
			<asp:Button id="Button1" Text="提交" runat="server" />
		</form>
	</body>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -