【发布时间】:2020-12-20 19:06:25
【问题描述】:
我将我的 cookie 保存为以下代码:
public static void SetCookie(string key, string value, int expireDay = 1)
{
var cookie = new HttpCookie(key , value);
cookie.Expires = DateTime.Now.AddDays(expireDay);
HttpContext.Current.Response.Cookies.Add(cookie);
}
读取 Cookie:
public static string GetCookie(string key)
{
string value = string.Empty;
var cookie = HttpContext.Current.Request.Cookies[key];
if (cookie != null)
{
if (string.IsNullOrWhiteSpace(cookie.Value))
{
return value;
}
value = cookie.Value;
}
return value;
}
问题是读取cookie时,所有值都是空的如下图:
【问题讨论】:
-
我刚刚意识到这不是一个 asp.net 核心问题。您能否改为检查控制器中 cookie 的值,看看是否有效?你能检查一下实际的http请求/响应,看看cookie是否真的被设置了吗?
-
好的,我该怎么做? @galdin
标签: asp.net-mvc asp.net-mvc-4 cookies key-value