[英]ASP.Net delete/expire session cookies
我們有許多內部公司的ASP.Net應用程序。 所有都使用表單身份驗證,並且都是基於會話的...
我要嘗試做的是,當用戶注銷一個應用程序時,他/她已注銷所有應用程序。
我有一些迭代cookie集合的邏輯。 我可以看到所有其他ASP.Net應用程序,但無法刪除它們。
我目前使用以下邏輯:
// expire all asp.net app tickets
string[] allDomainCookes = HttpContext.Current.Request.Cookies.AllKeys;
foreach (string domainCookie in allDomainCookes)
{
if (domainCookie.Contains("ASPXAUTH"))
{
var expiredCookie = new HttpCookie(domainCookie) { Expires = DateTime.Now.AddDays(-1) };
HttpContext.Current.Response.Cookies.Add(expiredCookie);
}
}
HttpContext.Current.Request.Cookies.Clear();
由於某些原因,它們沒有被刪除。 我知道它們都在那里,因為我已經將它們寫到了頁面上。 它們只是沒有被刪除。...是因為這些是會話cookie嗎?
我還應該添加它們是某個域的所有子域,因此所有權不應該成為問題嗎?
其實...我剛剛發現了問題。 我還需要指定域
string[] allDomainCookes = HttpContext.Current.Request.Cookies.AllKeys;
foreach (string domainCookie in allDomainCookes)
{
if (domainCookie.Contains("ASPXAUTH"))
{
var expiredCookie = new HttpCookie(domainCookie) {
Expires = DateTime.Now.AddDays(-1),
Domain = ".mydomain"
};
HttpContext.Current.Response.Cookies.Add(expiredCookie);
}
}
HttpContext.Current.Request.Cookies.Clear();
試試這個代碼..為我工作
FormsAuthentication.SignOut();
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
HttpContext.Current.Response.Cookies.Add(cookie1);
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
HttpContext.Current.Response.Cookies.Add(cookie2);
Cookies僅在同一域中工作。 如果是跨域的,則需要其他解決方案。 這是有關Asp.net Cookie的另一篇文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.