如何在cookie中存储字符串并检索它

时间:2011-12-13 11:45:18

标签: c# asp.net cookies

我想将用户名存储在cookie中,并在用户下次打开网站时检索它。是否可以创建一个在浏览器关闭时不会过期的cookie。我正在使用asp.net c#来创建网站。如何阻止浏览器提供保存用户名和密码

2 个答案:

答案 0 :(得分:32)

写一个cookie

HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;

// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire

// Add the cookie.
Response.Cookies.Add(myCookie);

Response.Write("<p> The cookie has been written.");

阅读cookie

HttpCookie myCookie = Request.Cookies["MyTestCookie"];

// Read the cookie information and display it.
if (myCookie != null)
   Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
else
   Response.Write("not found");

答案 1 :(得分:5)

除了Shai所说的,如果你以后想要更新相同的cookie使用:

URL = 'https://archive.ics.uci.edu/ml/databases/synthetic_control/synthetic_control.data'
sc <- read.table(URL, header = F, sep = "")
write.table(sc, file='synthetic_control.data')