Asp.net 正在尝试删除两个同名的Cookie
框架/语言:ASP.NET MVC# 问题:尝试删除两个名称相同但域不同的HttpOnly Cookie。一个cookie在.somedomain.com上,另一个在www.somedomain.com上 详情: 下面列出了使这些cookie过期的函数。它只使一个cookie过期,最后一个cookie-这似乎表明它正在被覆盖。这是预期的吗?有解决办法吗Asp.net 正在尝试删除两个同名的Cookie,asp.net,asp.net-mvc,cookies,Asp.net,Asp.net Mvc,Cookies,框架/语言:ASP.NET MVC# 问题:尝试删除两个名称相同但域不同的HttpOnly Cookie。一个cookie在.somedomain.com上,另一个在www.somedomain.com上 详情: 下面列出了使这些cookie过期的函数。它只使一个cookie过期,最后一个cookie-这似乎表明它正在被覆盖。这是预期的吗?有解决办法吗 private static void ExpireCookie(string cookieName) { var context =
private static void ExpireCookie(string cookieName)
{
var context = HttpContext.Current;
context.Response.Cookies.Remove(cookieName);
var cookieValue = string.Empty;
if (context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
var expirationTime = new DateTime(1999, 10, 12);
var cookie = new HttpCookie(cookieName, cookieValue)
{
HttpOnly = true,
Path = FormsAuthentication.FormsCookiePath,
Expires = expirationTime,
Secure = FormsAuthentication.RequireSSL,
Domain = ".somedomain"
};
context.Response.Cookies.Add(cookie);
//remove the cookieName on www.somedomain.com
var cookie2 = new HttpCookie(cookieName, cookieValue)
{
HttpOnly = true,
Path = FormsAuthentication.FormsCookiePath,
Expires = expirationTime,
Secure = FormsAuthentication.RequireSSL,
};
context.Response.Cookies.Add(cookie2);
}
为了提供一些结尾,我最后做了以下几点:
- 添加具有不同名称的第二个,以便将上面的
添加为cookie2
var cookie2 = new HttpCookie(cookieName +"2", cookieValue) { HttpOnly = true, Path = FormsAuthentication.FormsCookiePath, Expires = expirationTime, Secure = FormsAuthentication.RequireSSL, };
- 循环浏览所有Cookie并将
+cookieName
重命名回2
cookieName
for (var i = 0; i < currentResponse.Cookies.Count; i++) { var currentResponseCookie = currentResponse.Cookies[i]; if (currentResponseCookie != null && currentResponseCookie.Name == cookieName + "2") { currentResponse.Cookies[i].Name = cookieName; break; } }
for(变量i=0;i