我有一个添加到响应中的 cookie,但是如果已经存在具有相同密钥的 cookie,我想将其删除,否则我最终会得到 2 个相同密钥的 cookie。
我想通过简单地使 cookie 过期,它会从浏览器中删除它吗?
HttpCookie cookie = new HttpCookie("UserCookie");
cookie.Value = encTicket;
if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
ClearCookie("UserCookie");
HttpContext.Current.Response.Cookies.Add(cookie);
private static void ClearCookie(string key)
{
var httpContext = new HttpContextWrapper(HttpContext.Current);
var _response = httpContext.Response;
HttpCookie cookie = new HttpCookie(key)
{
Expires = DateTime.Now.AddMonths(-1),
Value = null
};
_response.Cookies.Add(cookie);
}
非常感谢任何帮助。