gpt4 book ai didi

c# - 如何读取 cookie

转载 作者:太空宇宙 更新时间:2023-11-03 20:13:17 24 4
gpt4 key购买 nike

我正在尝试保存一个 cookie 然后读取它。无法编译,因为行代码中存在错误 (readcookie[....] )-

Response.Write("USERNAME: " + readCookie["userInfo"]["userName"].ToString());

它说 - “string.this[int] 的最佳重载方法匹配具有无效参数。

如果我写,

 Response.Write("USERNAME: " + readCookie["userInfo"][0].ToString());

然后我就可以编译了。它在 save_click 上显示“数据已保存”消息,但在 read_click 上显示错误 - 对象引用无效。首先是否正确保存了 cookie。我怎样才能从中读取。这是我的代码:

protected void btnSaveCookie_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("userInfo");
myCookie.Values["userName"] = "Patrick";
myCookie.Values["lastVisit"] = DateTime.Now.ToString();
Response.Cookies.Add(myCookie);
Response.Write("Data Stored Into Cookie....");
}

protected void btnReadCookie_Click(object sender, EventArgs e)
{
HttpCookie readCookie = Request.Cookies.Get("userInfo");
Response.Write("USERNAME: " + readCookie["userInfo"]["userName"].ToString());
Response.Write("LAST VISIT: " + readCookie["userInfo"]["lastVisit"].ToString());
}

最佳答案

这是读取和写入 cookie 的辅助方法。

/// <summary>
/// Sets cookie.
/// </summary>
/// <param name="cookieName">Cookie name</param>
/// <param name="cookieValue">Cookie value</param>
/// <param name="days">Days to be expired</param>
public static void SetCookie(string cookieName, string cookieValue, int days)
{
try
{
var dt = DateTime.Now;

var cookie = new HttpCookie(cookieName);
cookie.Value = cookieValue;
cookie.Expires = dt.AddDays(days);

HttpContext.Current.Response.Cookies.Add(cookie);
}
catch (Exception ex)
{
// Log error
}
}

/// <summary>
/// Gets cookie string
/// </summary>
/// <param name="cookieName">Cookie name</param>
/// <returns>Cookie string</returns>
public static String GetCookie(String cookieName)
{
try
{
if (HttpContext.Current.Request.Cookies[cookieName] == null)
return String.Empty;

return HttpContext.Current.Request.Cookies[cookieName].Value;
}
catch (Exception ex)
{
// Log error
return String.Empty;
}
}

用法

protected void btnSaveCookie_Click(object sender, EventArgs e)
{
SetCookie("userName", "Patrick", 1); // Save for one day
SetCookie("lastVisit", DateTime.Now.ToString(), 1);
}

protected void btnReadCookie_Click(object sender, EventArgs e)
{
Response.Write("USERNAME: " + GetCookie("userName"));
Response.Write("LAST VISIT: " + GetCookie("lastVisit"));
}

原始问题的答案

如果您使用多值 Cookie(子键),您希望检索值作为 readCookie["xxx"]

protected void btnSaveCookie_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("userInfo");
myCookie.Values["userName"] = "Patrick";
myCookie.Values["lastVisit"] = DateTime.Now.ToString();
Response.Cookies.Add(myCookie);
Response.Write("Data Stored Into Cookie....");
}

protected void btnReadCookie_Click(object sender, EventArgs e)
{
HttpCookie readCookie = Request.Cookies.Get("userInfo");

Response.Write("USERNAME: " + readCookie["userName"]);
Response.Write("LAST VISIT: " + readCookie["lastVisit"]);
}

enter image description here

关于c# - 如何读取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18537568/

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