C# Cookie未删除
System.Web.UI.Page oPageMy cookie未被删除。我看了几篇文章,所有的东西看起来都很棒,就在我通过VisualStudio(或者在localhost下运行)点击按钮的时候,我的cookie仍然存在 不管它值多少钱,我都在使用Visual Studio 2012和.Net 4.0。我正在使用默认IE(Win7/64上的v9以及所有最新更新)在本地主机上调试C# Cookie未删除,c#,cookies,visual-studio-2012,devexpress,C#,Cookies,Visual Studio 2012,Devexpress,System.Web.UI.Page oPageMy cookie未被删除。我看了几篇文章,所有的东西看起来都很棒,就在我通过VisualStudio(或者在localhost下运行)点击按钮的时候,我的cookie仍然存在 不管它值多少钱,我都在使用Visual Studio 2012和.Net 4.0。我正在使用默认IE(Win7/64上的v9以及所有最新更新)在本地主机上调试 公共静态void登录用户(字符串strEmail、int-iId、int-iKeepDays) { HttpCoo
公共静态void登录用户(字符串strEmail、int-iId、int-iKeepDays)
{
HttpCookie oCookie=新的HttpCookie(“myookie”);
//设置cookie值。
oCookie.Secure=false;
oCookie[“Id”]=iId.ToString();
oCookie[“Email”]=strEmail;
oCookie.Expires=DateTime.Now.AddDays(iKeepDays);
//添加cookie。
HttpContext.Current.Response.Cookies.Add(oCookie);
}
public static void LogoutUser(System.Web.UI.Page oPage)
{
//去拿饼干。
HttpCookie oCookie=新的HttpCookie(“myookie”);
oCookie=HttpContext.Current.Request.Cookies[“mycokie”];
如果(空!=oCookie)
{
//取出饼干。
cookies.RemoveCookie(“myookie”);
//返回主页。
如果(oPage.IsCallback)
ASPxWebControl.RedirectOnCallback(“/”);
其他的
HttpContext.Current.Response.Redirect(“/”);
}
}
///
///此函数将用于删除cookies值
///
///
公共静态void RemoveCookie(字符串键)
{
//获取cookies值
HttpCookie oCookie=null;
if(null!=HttpContext.Current.Request.Cookies[key])
{
oCookie=HttpContext.Current.Request.Cookies[key];
//您不能直接删除cookie,您应该将其到期日期设置为更早的日期
oCookie.Expires=DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(oCookie);
}
}
cCookies.RemoveCookie(“myookie”)代码>行不调用您的RemoveCookie
方法。行应该是RemoveCookie(“myCookie”)代码>取而代之。答案似乎显而易见,因为我现在正在写它并弄明白了,但是我可以说,答案不是那么容易得到,不管是显而易见还是不显而易见
上面的代码在服务器上执行,但是cookie的删除发生在客户机上。执行必须转移到客户端,然后再返回到服务器,以便服务器能够识别cookie已被删除
我在同一个注销调用中读取数据,只是在一个不同的函数中。由于公认的实践状态是重置cookie,因此函数将cookie写回。cookie被删除,然后返回。它甚至有了一个新的文件名。(我打开了隐藏的cookie文件夹。)
我的解决方案是将登录状态传递给另一个函数。这解决了cookie部分。我只是发布您的评论。必须向下滚动才能看到RemoveCookie()方法。现在我真的很难受。我走进一行,上面写着DateTime.Now.AddDays(-1),但cookie仍然存在。@SarahWeinberger那么这是一个棘手的问题。从.Net代码中,您将无法看到cookie被删除。当.Net将响应头发送回浏览器时,您将看到一个带有过期Cookie日期的“Set Cookie”头。然后你会看到cookie被浏览器删除了。我们差不多在同一时间得出了相同的结论。谢谢你,大卫!
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);
}
}