2010-11-26 68 views
6

爲什麼這個cookie不保存在我的global.asax的Session_Start方法中?ASP.NET MVC cookie不保存?

//new anon user: 

var authCookie = new HttpCookie("user-id", string.Format("{0}-{1}", regiserAccountResponse.UserName, regiserAccountResponse.Key)) 
{ 
    Expires = DateTime.MaxValue, 
    Domain = "domain.com", 
    Secure = true, 
    HttpOnly = true 
}; 

//create the new users cookie - there's no need to call RegisterNewUserSession as this is done in the same call 
HttpContext.Current.Response.SetCookie(authCookie); 
+1

你是如何檢測是否它實際保存?有你是否使用過Wireshark來查看它是否會下降?你是否修復了域名並保護位以反映現實? – 2010-11-26 13:51:40

+0

我應該保留域名爲空,直到我準備好部署?或者它應該是本地主機,直到我部署? – iwayneo 2010-11-26 13:54:08

回答

5

如果您想限制Cookie到您網站的特定部分,則只需指定域名。只有在正確的範圍內,cookie纔會包含在請求中。

通過將域名設置爲「domain.com」,您意味着該Cookie將僅適用於「domain.com」,因此您不會從本地主機(或從域以外的任何其他域.COM「)。

您還會注意到,如果你試圖從比你自己以外的其他領域向瀏覽器發送一個cookie,瀏覽器會斌它。