c.net COOKIES setup tips on the WEB page
- 2020-05-12 02:27:10
- OfStack
1. The method of setting cookies is very simple. There are two ways:
1. Directly add Cookie value:
Response.Cookies["userName"] = "Tom";
"userName" Response. Cookies [] Expires = DateTime. Now. AddDays (1); \\ expiration time, cannot be viewed or called in Cookies file.
2. Create an instance of Cookie object:
HttpCookie cookie=new HttpCookie("userName");
cookie.Value = "Tom";
cookie.Expires = DateTime.Now.AddDays(1) ;
Response.Cookies.Add(aCookie)
Use any of the 1 methods to generate a file with the "userName" entry, which you can view in your Internet temporary folder.
You can also create and add Cookies with child keys, such as:
Response.Cookies["userInfo"]["userName"] = "Tom";
Or:
HttpCookie cookie=new HttpCookie("userInfo");
cookie.Values["userName"] = "Tom";
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie)
2. Retrieve Cookies:
The value of Cookies 1 key is:
Server.HtmlEncode(Request.Cookies["userInfo"]["userName"])
You can output it to the page using the Response.Write () method, such as:
Response. Write (Server HtmlEncode (Request Cookies [" userInfo "] [" userName "]));
Or assign values to other variables:
string strCookie1=Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);
All items and subkeys can be retrieved with the Cookies[i] array, such as:
3. Modify Cookies
If it is a numeric Cookie value, such as the number of visits, you can read it, add or subtract it, and then save it back. The 1-like modification can be directly saved into the new value.
4. Delete Cookies
To delete Cookies, just set the expiration date as invalid. For example, set the expiration date as 1 day when creating:
cookie.Expires = DateTime.Now.AddDays(1) ;
To delete, set as:
cookie.Expires = DateTime.Now.AddDays(-1) ;
Delete child key:
1. Directly add Cookie value:
Response.Cookies["userName"] = "Tom";
"userName" Response. Cookies [] Expires = DateTime. Now. AddDays (1); \\ expiration time, cannot be viewed or called in Cookies file.
2. Create an instance of Cookie object:
HttpCookie cookie=new HttpCookie("userName");
cookie.Value = "Tom";
cookie.Expires = DateTime.Now.AddDays(1) ;
Response.Cookies.Add(aCookie)
Use any of the 1 methods to generate a file with the "userName" entry, which you can view in your Internet temporary folder.
You can also create and add Cookies with child keys, such as:
Response.Cookies["userInfo"]["userName"] = "Tom";
Or:
HttpCookie cookie=new HttpCookie("userInfo");
cookie.Values["userName"] = "Tom";
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie)
2. Retrieve Cookies:
The value of Cookies 1 key is:
Server.HtmlEncode(Request.Cookies["userInfo"]["userName"])
You can output it to the page using the Response.Write () method, such as:
Response. Write (Server HtmlEncode (Request Cookies [" userInfo "] [" userName "]));
Or assign values to other variables:
string strCookie1=Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);
All items and subkeys can be retrieved with the Cookies[i] array, such as:
string[] cooName = new string[Request.Cookies.Count];
string[] cooValue = new string[Request.Cookies.Count];
HttpCookie aCookie;
for(int i=0;i<Request.Cookies.Count;i++){
aCookie = Request.Cookies[i];
cooName[i] = Server.HtmlEncode(aCookie.Name);
if(!aCookie.HasKeys){
cooValue[i] = Server.HtmlEncode(aCookie.Value);
}else{
string[] subcooName = new string[aCookie.Values.Count];
string[] subcooValue = new string[aCookie.Values.Count];
for(int j=0;j<aCookie.Values.Count;j++){
subcooName[j] = Server.HtmlEncode(aCookie.Values.AllKeys[j]);
subcooValue[j] = Server.HtmlEncode(aCookie.Values[j]);
}
}
}
3. Modify Cookies
If it is a numeric Cookie value, such as the number of visits, you can read it, add or subtract it, and then save it back. The 1-like modification can be directly saved into the new value.
4. Delete Cookies
To delete Cookies, just set the expiration date as invalid. For example, set the expiration date as 1 day when creating:
cookie.Expires = DateTime.Now.AddDays(1) ;
To delete, set as:
cookie.Expires = DateTime.Now.AddDays(-1) ;
Delete child key:
HttpCookie cookie;
cookie = Request.Cookies["userInfo"];
aCookie.Values.Remove("userName");
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);