2015-11-11 23 views
1

我正在使用ashx文件来终止用户会话并重定向到主页面。重定向不起作用以及例程过期cookies。 我试过HttpContext.Current.Response.Redirect(“index.aspx”,false),但也没有工作。有什么更好的办法谢谢。过期的Cookie和重定向

我的代码:

public void ProcessRequest (HttpContext context) 
{ 
    HttpCookie cookie = HttpContext.Current.Request.Cookies["user"]; 
    if (cookie != null) 
     HttpContext.Current.Request.Cookies["user"].Expires = DateTime.Now.AddYears(-30); 
    cookie = HttpContext.Current.Request.Cookies["admin"]; 
    if (cookie != null) 
     HttpContext.Current.Request.Cookies["admin"].Expires = DateTime.Now.AddYears(-30); 
    HttpContext.Current.Response.Redirect("index.aspx", false);  
} 
+0

你是怎么调用这个Generic Handler(.ashx)的?你通过ajax打电话吗? –

+0

是的,通过一个包含ajax命令来调用logout.ashx的javascript例程。 注销功能() { $就({ URL: “logout.ashx” 的contentType: “应用程序/ JSON;字符集= UTF-8” 数据类型: “HTML” }) } – Luana

+0

我看到一个类似的帖子:http://stackoverflow.com/questions/17866856/trying-to-redirect-from-an-ashx-page-to-an-aspx-page –

回答

0

如果你的处理器返回302结果AJAX调用不会自动地将用户,它会返回重定向到页面的内容。

如果没有异常,您应该让处理程序过期并返回true,否则返回false。在ajax调用的成功回调中,如果结果为true,则通过javascript将用户重定向到下一页。如果为false,则向他们展示某种错误消息。

对于cookie过期,您必须从请求中获取cookie,将过期时间设置为过去的时间(就像您当前所做的那样),但有一件事没有做的是将cookie添加回响应。