2009-11-09 47 views
1
System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Domain = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString().ToLower(); 
System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Value = tokenID.ToString(); 
System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Path = "~/"; 
System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Expires = DateTime.Now.AddDays(7); 

現在當我的web應用程序中,當用戶單擊註銷以使該cookie被銷燬時,我會做什麼代碼?下面是一些使cookie成爲可能的代碼,你能告訴我如何取消它嗎?

注意我想這已經與和沒有註釋行,這是行不通的:

//System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Domain = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString().ToLower(); 
    //System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Value = tokenID.ToString(); 
    //System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Path = "~/"; 
    System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Expires = DateTime.Now.AddDays(-1); 
+9

System.Web.HttpContext.Current.Response.Cookies [「ssocookies」]。Value =「O hai!我給你貼了一個cookie但是我吃了它」 – hobbs 2009-11-09 20:44:57

+0

事實證明,最初的代碼甚至沒有正確地創建一個餅乾...... – BigOmega 2010-05-04 18:34:22

回答

2

的方法FormsAuthentication.SignOut做它是這樣的:

HttpCookie cookie = new HttpCookie(FormsCookieName, str); 
    cookie.HttpOnly = true; 
    cookie.Path = _FormsCookiePath; 
    cookie.Expires = new DateTime(1999, 10, 12); 
    cookie.Secure = _RequireSSL; 
    if (_CookieDomain != null) 
    { 
     cookie.Domain = _CookieDomain; 
    } 
    current.Response.Cookies.RemoveCookie(FormsCookieName); 
    current.Response.Cookies.Add(cookie); 

在任何情況下,使用類似Fiddler檢查你的HTTP流量應該給你一個線索,這是怎麼回事。

5

我做的是重新進行設置,且空值和過去的到期日:

var context = System.Web.HttpContext.Current; 

context.Response.Cookies["ssocookies"].Domain = context.Request.ServerVariables["SERVER_NAME"].ToString().ToLower(); 
context.Response.Cookies["ssocookies"].Value = ""; 
context.Response.Cookies["ssocookies"].Path = "~/"; 
context.Response.Cookies["ssocookies"].Expires = DateTime.Now.AddDays(-1); 
+0

+1這是一個好主意! – 2009-11-09 20:44:50

+0

看到我的新編輯問題 – BigOmega 2009-11-09 20:47:27

+0

它應該工作,我會檢查瀏覽器如何實際存儲cookie。檢查一下,它不會一直受到損壞。 – 2009-11-09 20:51:35

2

通過過去設置其到期時間來使cookie過期。

 System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Domain = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString().ToLower(); 
     System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Value = tokenID.ToString(); 
     System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Path = "~/"; 
     System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Expires = DateTime.Now.AddDays(-7); 
+0

嘗試過,未能正常工作 – BigOmega 2009-11-09 20:46:30

0
System.Web.HttpContext.Current.Response.Cookies.Remove("ssocookies"); 
相關問題