【问题标题】:Why I am losing cookies and session on Ajax requests in IE 9为什么我在 IE 9 中丢失了有关 Ajax 请求的 cookie 和会话
【发布时间】:2015-09-01 08:04:56
【问题描述】:

会话 cookie 在 Chrome 和 Firefox 中运行良好,但对于 IE9 和 AJAX 请求,我会丢失所有会话 cookie。

直接请求查看

  public class AddressController : Controller
  {
    [MvcSiteMapNode(Title = "Addresses", ParentKey = "MyAccount", Key = "Addresses")]
     public ActionResult Index()
     {
        ....
         var memberId = GetKeyValues.GetMemberId(); // This works perfect.
        ...
      }

Ajax 调用

   $.ajax({
        url: "/Address/CheckPrimaryAddressGood?t="+ Math.random(),
        type: "Get",
        success: function(data) {
         ...

public class AddressController : Controller
{
    public ActionResult CheckPrimaryAddressGood()
        {
           ...
           var memberId = GetKeyValues.GetMemberId();
           ...
       }
 }
 public static class GetKeyValues
 {
    public static string GetMemberId()
    {
         if (HttpContext.Current.Session[keyCookie] != null)
            {
                memberId = GetMemberIdFromSession();
            }
            else if (HttpContext.Current.Request.Cookies["token"] != null)
            {
                memberId = GetMemberIdFromCookie();
            }
    }
}

从 AJAX 调用中,我只丢失了 IE9 的 cookie 值。我尝试了 P3P 覆盖仍然无法从这篇文章P3P link

有人遇到过类似的问题吗?请让我知道如何解决这个问题。我已经花了一天的时间。

编辑

我刚刚在 Fiddler 中跟踪 IE 没有发送 Header 数据它只是发送 "Connection=Keep-Alive&Pragma=no-cache&Accept=*%2f*&Accept-Encoding=gzip%2c+deflate&Accept-Language=en-US&Host=ebiz.company.com%3a28712&User-Agent=Mozilla%2f5.0+(compatible%3b+MSIE+9.0%3b+Windows+NT+6.1%3b+WOW64%3b+Trident%2f5.0)&Origin=http%3a%2f%2febiz.spe.org%3a28712}

但 Chrome:{Connection=keep-alive&Accept=*%2f*&Accept-Encoding=gzip%2c+deflate%2c+sdch&Accept-Language=en-US%2cen%3bq%3d0.8&Cookie=ASP.NET_SessionId%3d2a4tr1ymierclqsfxyfahqbc%3b+__session%3a0.5654769616667181%3ashowwarning%3dtrue%3b+__session%3a0.5654769616667181%3aBadAddressWarning%3dfalse%3b+ ....

为什么?

【问题讨论】:

  • 这可能是 IE 中的安全区域的问题。因此,如果您的连接是 http 并且请求是 https,它将使用另一组 Cookie(或多或少是另一个用户上下文)。
  • 这里已经有类似的问题了,看看这里:stackoverflow.com/questions/389456/…

标签: jquery ajax asp.net-mvc asp.net-mvc-4 cookies


【解决方案1】:

这些只是一些想法,可能会有所帮助(您现在可能已经阅读或尝试过这些想法)。似乎没有灵丹妙药。

其他一些问题也有类似的问题,但似乎并不完全是您的问题(尤其是在您尝试 P3P 之后)。互联网上也有很多一般的帖子,都是围绕着同样的几个问题。

No Session Cookies on Internet Explorer 9 AJAX requests

Cookie blocked/not saved in IFRAME in Internet Explorer

一些想法:

  • 一个答案在 url 中存在下划线问题。你没有 那个,但是你可以尝试一个没有随机参数的干净的吗?只是 以防它不喜欢那样。
  • 很多关于这样做麻烦的帖子 从 iframe 中。如果你没有 iframe,这不是 问题。
  • P3P,你说你试过了;我看到一条评论说标题 必须针对每个请求进行设置,而不仅仅是那些正在寻找的请求 会话/cookies。
  • 跨域/CORS 问题?看起来不像 使用您的根相对网址。
  • 在另一台计算机上尝试 IE9?愚蠢的, 但也许这是您浏览器上的一些晦涩设置;区域等。
  • fiddler 是否在您网站上浏览的常规页面上显示会话 ID? (只是为了确保它不是站点范围的,而只是这个 ajax 调用)。

  • 我通常发布 ajax 而不是 Get(只是有很多数据),然后做 有会话工作。这也避免了需要缓存清除 随机参数。

  • 我正在使用旧的 Web 表单而不是 mvc,并发布到 asmx。在 asmx方法,我需要修饰一下服务端方法。

    // ScriptService and ScriptMethod are required for the jquery.ajax() call. They weren't required for jquery.post(). WebMethod needed for session.
    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public string DoSomething() ...
    

【讨论】:

  • 完美总结,谢谢。我的页面中有 Iframe,并且几天来一直在尝试设置正确的 P3P 标头,直到我发现我的测试域中有下划线 =-O Long live IE... 其他地方。
【解决方案2】:

您是否考虑过使用 sessionStorage?看看火狐吧

https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

对于所有其他浏览器:

https://code.google.com/p/sessionstorage/

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2014-08-14
    • 2016-01-15
    相关资源
    最近更新 更多