Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operation methods of ASP.NET to Cookie?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the operation methods of ASP.NET to Cookie". In the daily operation, I believe that many people have doubts about the operation methods of ASP.NET to Cookie. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the operation methods of ASP.NET to Cookie?" Next, please follow the editor to study!

Overview

Cookie is used to save the request information of the server page requested by the client browser.

We can store non-sensitive user information, and the preservation time can be set as needed. If the Cookie expiration date is not set, its life cycle is saved until the browser is closed, and the Expires property of the Cookie object is set to MinValue to indicate that it never expires.

The amount of data stored in Cookie is limited, and most browsers are 4K, so don't store big data.

Since not all browsers support Cookie, the data will be saved on the client in clear text.

Create a Cookie: send it to the client browser

All Cookie that are the same as Domain and Path exist in one file on the client side.

/ / General setting Response.Cookies ["userName"] .Value = "Park"; Response.Cookies ["userName"] .Expires = DateTime.Now.AddDays (1); / / expired Response.Cookies ["userName"]. Domain = "park.aa.com" if the browser is closed by default without Expires; / / Domain defaults to the domain name part to represent all subdomains under aa.com. Response.Cookies ["userName"]. Path = "App1"; / / path defaults to the root directory "/", indicating all pages and subdirectories under the root directory / / single-valued CookieHttpCookie Cookie = new HttpCookie ("userName"); cookie.Value = "Park"; cookie.Expires = DateTime.Now.AddDays (1); Response.Cookies.Add (cookie); / / Multi-valued CookieHttpCookie cookies = new HttpCookie ("userName"); cookies ["name"] = "Park"; cookies ["sex"] = "1" Cookies.Expires = DateTime.Now.AddMinutes (20); Response.Cookies.Add (cookies); / / Response.SetCookies (cookies) / / Response.AppendCookis (cookies); second, read Cookie:

Domain, Path, and Expires are unreadable

If (Request.Cookies ["userName"]! = null) {/ / read multi-valued Cookie Response.Write (Request.Cookies ["userName"] .Value) / can Server.HtmlEncode () encode / / read multi-valued Cookie Response.Write ("the key value in Cookie is userid:" + Request.Cookies ["userName"] ["sex"]);} III. Modify Cookie

Instead of directly modifying a Cookie, you create a Cookie with the same name and send the Cookie to the browser overriding the old Cookie on the client.

HttpCookie cok = Request.Cookies ["userName"]; / / get the client Cookie object if (cok! = null) {cok.Values ["userid"] = "alter-value"; / / modify Cookie cok.Values.Set ("newid", "newValue"); / / add new content Response.AppendCookie (cok) to Cookie; / / or Response.Cookies ["userName"]. Value = "aa" } Response.Cookies ["Porschev"] .Expires = DateTime.Now.AddMinutes (- 1); fourth, delete Cookie:

You cannot delete a Cookie directly, but by changing its Expires to some time in the past, the browser will delete the expired Cookie.

Response.Cookies ["userName"]. Expires = DateTime.Now.AddDays (- 1); / / or HttpCookie cok = Request.Cookies ["userName"]; if (cok! = null) {if (! CheckBox1.Checked) {cok.Values.Remove ("userid"); / / remove the value with userid key} else {TimeSpan ts = new TimeSpan (0,0,0,0); cok.Expires = DateTime.Now.Add (ts) / / delete the entire Cookie, as long as the expiration time is now set to} Response.AppendCookie (cok);} at this point, the study of "what is the operation method of ASP.NET to Cookie" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report