請參閱will寶哥文章 鬼打牆事件之『ASP.NET 無法刪除 Cookie 的問題』
摘錄重點如下:
- 若要清除跨 Domain 的 Cookie 必須清除兩次,例如說使用者在 www1.domain.com 要執行登出動作,必須要先將 Domain 為 www1.domain.com 的 Cookie 給清除掉,在接著將 Domain 為 .domain.com 的這個 Cookie 清除掉。
- 因為這兩個 Cookie 為「同名」(主網域與子網域),全部都叫做 Token(will哥範例的cookie名稱),所以無法在一個 HTTP Request 中清除掉兩個同名的 Cookie,所以必須要在不同的兩個 HTTP Request 中個別刪除不同 Domain 的 Cookie。
this.Session.Clear();
if (this.Request.Cookies["StutXD"] != null)
{
SSO.SSOCookie cookie = new SSO.SSOCookie();
//重設cookie的存活時間
HttpCookie reset = cookie.LogOutCookie(this.Request.Cookies["StutXD"]);
if (this.Request["domain"] == null)
{
this.Response.SetCookie(reset);
this.Response.Redirect("logout.aspx?domain=1");
}
else
{
//清除二次才行
reset.Domain = ".stut-xd.tw";
this.Response.SetCookie(reset);
this.Response.Redirect("login.aspx");
}
}
else
this.Response.Redirect("login.aspx");
在測試重新設定cookie的存活時間發現的問題Orz,
當第一次建立cookie時的expires時間是有辦法print,
因為在http header的set-cookie是可見到expires的設定的時間,
不然expires print出來的時間擷取是異常的顯示時間。
測試程式碼如下:
public partial class demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.Request.Cookies["StutXD"] != null)
{
SSO.SSOCookie cookie = new SSO.SSOCookie();
string token = cookie.GetCookieValue(this.Request.Cookies["StutXD"], "token");
HttpCookie stutXD = this.Request.Cookies["StutXD"];
stutXD.Domain = ".stut-xd.tw";
this.Response.Write(token + "
" + stutXD.Expires);
}
else
this.Response.Write("no cookie");
}
}
執行結果:588e7d90-838b-4e7e-b148-db6c65ebbb0e
0001/1/1 上午 12:00:00
101219補充:
之前發現在子網域(a.my.tw)產生主網域(my.tw)共用的cookie發現,在update cookie的expire time的時候,忘記設定cookie 的 domain(a.my.tw),導致子網域產生與主網域同樣域名的cookie,導致其他子網域(b.my.tw)登出清除認證cookie時,原先的a.my.tw的cookie是無法清除的,使得無法所有系統同時登出。
附圖一張

沒有留言:
張貼留言
留個話吧:)