This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21251|Reply: 1

[ASP.NET] asp.net store cookies on the client and save them

[Copy link]
Posted on 4/2/2015 10:44:46 AM | | | |


Front Desk Code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cook.aspx.cs" Inherits="itsvse.cook" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
  11.         <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br />
  12.         <asp:Button ID="Button1" runat="server" Text="存武软论坛cookies"
  13.             onclick="Button1_Click" />
  14.     </div>
  15.     </form>
  16. </body>
  17. </html>
Copy code



Background Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. namespace itsvse
  8. {
  9.     public partial class cook : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {

  13.             if (Request.Cookies["UserInfo"] != null)
  14.             {

  15.                 //读取Cookie 键值
  16.                 txtUserName.Text = Request.Cookies["UserInfo"]["username"].ToString();
  17.                 txtPassword.Text = Request.Cookies["UserInfo"]["pwd"].ToString();
  18.             }


  19.             
  20.             

  21.         }

  22.         protected void Button1_Click(object sender, EventArgs e)
  23.         {
  24.             if (Request.Cookies["UserInfo"] == null)
  25.             {
  26.                 //创建Cookie 并命名
  27.                 HttpCookie cookietwo = new HttpCookie("UserInfo");
  28.                 //   cookietwo.Name = "UserInfo";
  29.                 //Cookie为一年有效期
  30.                 cookietwo.Expires = DateTime.Today.AddDays(360);
  31.                 //设置Cookie对应键值
  32.                 cookietwo.Values["username"] = txtUserName.Text;
  33.                 cookietwo.Values["pwd"] = txtPassword.Text;
  34.                 //  CookieHelp.CookieHelper.AddCookie(cookietwo);
  35.                 // 用Response添加到Cookie中
  36.                 Response.Cookies.Add(cookietwo);
  37.             }
  38.         }
  39.     }
  40. }
Copy code






Previous:ASP.NET knowledge points
Next:Super dick! Android root-free hyperterminal
 Landlord| Posted on 4/2/2015 11:07:23 AM |
There are some bugs in the code, if there is a cookie, it cannot be replaced, you need to delete the judgment code of the button, and then add if (!) when loading the page. IsPostBack)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com