我正在測試以查看用戶是否打開了Cookie,並且我似乎沒有正確地做某件事。我的Cookie測試不起作用
這裏是我的測試:
private bool CookiesAllowed()
{
var testCookie = new HttpCookie("TestCookie", "abcd");
System.Web.HttpContext.Current.Response.Cookies.Set(testCookie);
var tst = System.Web.HttpContext.Current.Request.Cookies.Get("TestCookie");
if (tst == null || tst.Value == null)
{
return false;
}
return true;
}
我把一個cookie在那裏......然後得到它回來。但它總是成功。
這裏是我如何禁用它們:
我登錄到Gmail,它告訴我,我的cookie被禁用,所以我相信我做的那部分權利。
我在做什麼錯?
編輯
要回答詹姆斯的問題,我從我的登錄屏幕,這是我的輸入畫面作爲第一個檢查調用此:
public ActionResult LogOn(string id)
{
if (!CookiesAllowed())
{
return View("CookiesNotEnabled");
}
另外,我還測試了這個活,在視覺工作室之外,而不是在本地主機,它做了同樣的事情。
你在哪裏調用這段代碼? – James
也許你應該看看[最好的方式來確定是否在ASP.NET中啓用cookie?](http://stackoverflow.com/questions/210321/best-way-to-determine-if-cookies-are- enabled-in-asp-net) –