System.Web.UI.Page oPage我的 cookie 没有被删除。我看了几篇文章,一切看起来都很棒,就在我单步通过 Visual Studio(或只是在 localhost 下运行)以响应单击按钮时,我的 cookie 仍然存在。
不管它值多少钱,我都在使用带有 .Net 4.0 的 Visual Studio 2012。我正在使用默认 IE(Win7/64 上的 v9 以及所有最新更新)在 localhost 上进行调试。
public static void LoginUser(String strEmail, int iId, int iKeepDays)
{
HttpCookie oCookie = new HttpCookie("myCookie");
// Set the cookie value.
oCookie.Secure = false;
oCookie["Id"] = iId.ToString();
oCookie["Email"] = strEmail;
oCookie.Expires = DateTime.Now.AddDays(iKeepDays);
// Add the cookie.
HttpContext.Current.Response.Cookies.Add(oCookie);
}
public static void LogoutUser(System.Web.UI.Page oPage)
{
// Get the cookie.
HttpCookie oCookie = new HttpCookie("myCookie");
oCookie = HttpContext.Current.Request.Cookies["myCookie"];
if (null != oCookie)
{
// Remove the cookie.
cCookies.RemoveCookie("myCookie");
// Go back to the home page.
if (oPage.IsCallback)
ASPxWebControl.RedirectOnCallback("/");
else
HttpContext.Current.Response.Redirect("/");
}
}
/// <summary>
/// This function will be used to remove cookies value
/// </summary>
/// <param name="key"></param>
public static void RemoveCookie(String key)
{
//get cookies value
HttpCookie oCookie = null;
if (null != HttpContext.Current.Request.Cookies[key])
{
oCookie = HttpContext.Current.Request.Cookies[key];
// You cannt directly delte cookie you should set its expiry date to earlier date
oCookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(oCookie);
}
}