在线等,急用.ashx中处理cookie.结贴就80分

坂田银时123 2014-05-22 06:37:15

小弟在做一份大作业,使用了ashx处理用户退出后清理cookies但发现怎么处理都清除不了
包款已经处理的浏览器缓存.都清理的了.但是还是丝毫没有效果

之前cookies是ashx写入的,而且经测试本次ashx也是可以写入cookies的.
问题就是ashx文件改写和清除不了cookies
...全文
606 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
坂田银时123 2014-05-23
  • 打赏
  • 举报
回复
引用 1 楼 save4me 的回复:
给cookie设置过期时间,参数下面的代码: /// This function will be used to remove /// cookies value /// </summary> /// <param name="key"></param> /// <returns>Cookies value</returns> public static void RemoveCookie(string key) { //get cookies value HttpCookie cookie = null; if (HttpContext.Current.Request.Cookies[key] != null) { cookie = HttpContext.Current.Request.Cookies[key]; //You can't directly delte cookie you should //set its expiry date to earlier date cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } }
引用 4 楼 haof3344 的回复:
关键在 cookie.Expires = DateTime.Today.AddYears(-1); 这样就可以把之前的设置给覆盖掉
引用 3 楼 save4me 的回复:
Building a Web 2.0 Portal with ASP.NET 3.5中第98页有个Logout的示例

using System;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
public class Logout : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        /// Expire all the cookies so browser visits us as a brand new user
        List<string> cookiesToClear = new List<string>( );
        foreach (string cookieName in context.Request.Cookies)
        {
            HttpCookie cookie = context.Request.Cookies[cookieName];
            cookiesToClear.Add(cookie.Name);
        }
        foreach (string name in cookiesToClear)
        {
            HttpCookie cookie = new HttpCookie(name, string.Empty);
            cookie.Expires = DateTime.Today.AddYears(-1);
            context.Response.Cookies.Set(cookie);
        }
        context.Response.Redirect("~/Default.aspx");
    }
	
    public bool IsReusable {
        get {
            return true;
        }
    }
}
引用 2 楼 zhouxiulu 的回复:
我非常严重的怀疑你写Cookie的位置和你清除Cookie的位置不一致
首先都谢谢大家这么积极地帮我解决问题!问题解决了,但都不是上诉的问题....只是我设置了path后,导致不能在二级域名下删除..这个细节死人啊...搞了那么久
save4me 2014-05-23
  • 打赏
  • 举报
回复
引用 5 楼 u012332962 的回复:
[quote=引用 1 楼 save4me 的回复:] 给cookie设置过期时间,参数下面的代码: /// This function will be used to remove /// cookies value /// </summary> /// <param name="key"></param> /// <returns>Cookies value</returns> public static void RemoveCookie(string key) { //get cookies value HttpCookie cookie = null; if (HttpContext.Current.Request.Cookies[key] != null) { cookie = HttpContext.Current.Request.Cookies[key]; //You can't directly delte cookie you should //set its expiry date to earlier date cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } }
引用 4 楼 haof3344 的回复:
关键在 cookie.Expires = DateTime.Today.AddYears(-1); 这样就可以把之前的设置给覆盖掉
引用 3 楼 save4me 的回复:
Building a Web 2.0 Portal with ASP.NET 3.5中第98页有个Logout的示例

using System;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
public class Logout : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        /// Expire all the cookies so browser visits us as a brand new user
        List<string> cookiesToClear = new List<string>( );
        foreach (string cookieName in context.Request.Cookies)
        {
            HttpCookie cookie = context.Request.Cookies[cookieName];
            cookiesToClear.Add(cookie.Name);
        }
        foreach (string name in cookiesToClear)
        {
            HttpCookie cookie = new HttpCookie(name, string.Empty);
            cookie.Expires = DateTime.Today.AddYears(-1);
            context.Response.Cookies.Set(cookie);
        }
        context.Response.Redirect("~/Default.aspx");
    }
	
    public bool IsReusable {
        get {
            return true;
        }
    }
}
引用 2 楼 zhouxiulu 的回复:
我非常严重的怀疑你写Cookie的位置和你清除Cookie的位置不一致
首先都谢谢大家这么积极地帮我解决问题!问题解决了,但都不是上诉的问题....只是我设置了path后,导致不能在二级域名下删除..这个细节死人啊...搞了那么久[/quote] 谢谢分享结果
来一脚 2014-05-22
  • 打赏
  • 举报
回复
关键在 cookie.Expires = DateTime.Today.AddYears(-1); 这样就可以把之前的设置给覆盖掉
save4me 2014-05-22
  • 打赏
  • 举报
回复
Building a Web 2.0 Portal with ASP.NET 3.5中第98页有个Logout的示例

using System;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
public class Logout : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        /// Expire all the cookies so browser visits us as a brand new user
        List<string> cookiesToClear = new List<string>( );
        foreach (string cookieName in context.Request.Cookies)
        {
            HttpCookie cookie = context.Request.Cookies[cookieName];
            cookiesToClear.Add(cookie.Name);
        }
        foreach (string name in cookiesToClear)
        {
            HttpCookie cookie = new HttpCookie(name, string.Empty);
            cookie.Expires = DateTime.Today.AddYears(-1);
            context.Response.Cookies.Set(cookie);
        }
        context.Response.Redirect("~/Default.aspx");
    }
	
    public bool IsReusable {
        get {
            return true;
        }
    }
}
zhouxiulu 2014-05-22
  • 打赏
  • 举报
回复
我非常严重的怀疑你写Cookie的位置和你清除Cookie的位置不一致
save4me 2014-05-22
  • 打赏
  • 举报
回复
给cookie设置过期时间,参数下面的代码:

/// This function will be used to remove
/// cookies value
/// </summary>
/// <param name="key"></param>
/// <returns>Cookies value</returns>
public static void RemoveCookie(string key)
{
//get cookies value
HttpCookie cookie = null;
if (HttpContext.Current.Request.Cookies[key] != null)
{
cookie = HttpContext.Current.Request.Cookies[key];
//You can't directly delte cookie you should
//set its expiry date to earlier date
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(cookie);
}


}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧