gpt4 book ai didi

c# - 如何将字符串存储在 cookie 中并检索它

转载 作者:可可西里 更新时间:2023-11-01 07:49:50 27 4
gpt4 key购买 nike

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

最佳答案

写一个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");

关于c# - 如何将字符串存储在 cookie 中并检索它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8488657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com