简体   繁体   English

使用IE11的CORS请求

[英]CORS request with IE11

I have a CORS (cross origin resource sharing) request coming from my login page to the application site, on a different URL. 我有一个CORS(跨源资源共享)请求从我的登录页面到应用程序站点,在不同的URL上。 I have a simple page I ping to determine if a user is already logged in, and if so, redirects them. 我有一个简单的页面我ping以确定用户是否已经登录,如果是,则重定向它们。 Otherwise I show a login page. 否则我会显示一个登录页面。 I use jQuery. 我使用jQuery。

This works great in safari, chrome, firefox... and not IE (naturally). 这适用于safari,chrome,firefox ......而不是IE(自然)。 According to MS, IE 10 and later should support CORS requests with withCredentials 根据MS,IE 10及更高版本应该使用withCredentials支持CORS请求

I'm using jquery-2.0.3.min.js 我正在使用jquery-2.0.3.min.js

Any ideas why this isn't working in IE11? 任何想法为什么这不适用于IE11?

EDIT: It appears as though it IS partially working, as it is now returning a value of {"id":false}. 编辑:它似乎部分工作,因为它现在返回值{“id”:false}。 This happens every time, meaning that the server is never getting the credentials. 每次都会发生这种情况,这意味着服务器永远不会获得凭据。 I am also posting my is_logged_in page, I am using the code igniter framework. 我也发布了我的is_logged_in页面,我正在使用代码点火器框架。

EDIT: After enabling "Allow data sources across domains" under IE's security settings, I no longer receive any error messages. 编辑:在IE的安全设置下启用“允许跨域的数据源”后,我不再收到任何错误消息。

The exact error I receive is: 我收到的确切错误是:

SEC7118: XMLHttpRequest for http://mysite.net/guest/is_logged_in required Cross Origin Resource Sharing (CORS). SEC7118:用于http://mysite.net/guest/is_logged_in的 XMLHttpRequest需要跨源资源共享(CORS)。

$.ajax({
url: 'http://mysite.net/guest/is_logged_in',
type: 'POST',
crossDomain: true,
xhrFields: {
       withCredentials: true
  },

dataType: 'json',
success: function(data) {

    if(data.id) {
        window.location.replace("http://mysite.net");
    }
}
});

and

public function is_logged_in()
{
    $allowed = array(
        'http://mysite.net',
        'http://www.mysite.net',
        'http://www.mysite.com',
    );

    $url = $_SERVER['HTTP_REFERER'];
    $url = substr($url, 0, strpos($url, '/', 8));
    if(isset($_SERVER['HTTP_ORIGIN']))
    {
        if(in_array($_SERVER['HTTP_ORIGIN'], $allowed))
        {
            $this->output->set_header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
        }
    }
    else
    {
        if(in_array($url, $allowed))
        {
            $this->output->set_header('Access-Control-Allow-Origin: ' . $url);
        }
    }


    $this->output->set_header('Access-Control-Allow-Headers: X-Requested-With');
    $this->output->set_header('Access-Control-Allow-Credentials: true');
    $this->output->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");


    //TODO: Try to detect if this is an ajax request, and disallow it if not.

    $data = new stdClass();
    $this->load->library("ion_auth");
    if($this->ion_auth->logged_in())
    {
        $data->name = $this->ion_auth->user()->row()->first_name;
        $data->id = $this->ion_auth->get_user_id();
    } else {
        $data->id = false;
    }

    $this->output->set_output(json_encode($data));

}

Thanks in advance 提前致谢

Changing the setting for "Access data sources across domains" to Enabled turns off cross-domain checks in IE and is horrifically unsafe. 将“跨域访问数据源”的设置更改为“已启用”将关闭IE中的跨域检查,并且非常不安全。 Instead, you need to ensure that the target 3rd-party resource sends a valid P3P policy that indicates that it's not doing horrible things to the user's privacy. 相反,您需要确保目标第三方资源发送有效的P3P策略 ,该策略指示它不会对用户的隐私做出可怕的事情。

Found the issue. 发现了这个问题。

I had a similar problem (using CORS in general, not specifically GWT). 我遇到了类似的问题(一般使用CORS,而不是GWT)。 It turned out that the browser settings were blocking third-party cookies (IE10 > Internet Options > Privacy > Advanced > Third Party Cookies > Accept). 事实证明,浏览器设置阻止了第三方cookie(IE10> Internet选项>隐私>高级>第三方Cookie>接受)。 To solve the problem, I checked "Override automatic cookie handling", "Accept" (Third-party Cookies) and "Always allow session cookies." 为了解决这个问题,我检查了“覆盖自动cookie处理”,“接受”(第三方Cookie)和“始终允许会话cookie”。

Andrew answered this question here: CORS doesn't work with cookies in IE10 Andrew在这里回答了这个问题: CORS在IE10中不适用于cookie

EDIT: (Now that I know what to search for) This question may yield some help too, if anyone else runs into this issue. 编辑:(现在我知道要搜索什么)这个问题可能会产生一些帮助,如果其他人遇到这个问题。 Internet Explorer 10 is ignoring XMLHttpRequest 'xhr.withCredentials = true' Internet Explorer 10忽略XMLHttpRequest'xhr.withCredentials = true'

我们遇到的问题是,除了所有其他浏览器之外,IE11发送Access-Control-Request-Headers: accept请求,因此必须将“accept”添加到allowedHeaders cors配置中,因为它似乎不属于默认弹簧cors配置。

IE10 requires the server return a valid P3P policy in addition to CORS headers for cross domain requests. IE10要求服务器返回有效的P3P策略以及用于跨域请求的CORS头。 Here is sample php code for returning a P3P header from the server. 这是用于从服务器返回P3P标头的示例php代码。

  $szOrigin = $_SERVER['HTTP_ORIGIN'];
  if ($szOrigin != null)
  {
        header("Access-Control-Allow-Origin: $szOrigin");
        header("Access-Control-Allow-Credentials: true");
        header("P3P: CP=\"ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI\"");
  }

After a lot of digging i found that the page that i'm pinging using ajax is on the internet zone while my current page in on the intranet zone. 经过大量的挖掘,我发现我正在使用ajax ping的页面在Internet区域,而我的当前页面在Intranet区域。

IE 11 has the "Protected Mode" enabled for internet sites and when this is enabled cookies are not being sent to the site that i'm pinging even if they belong to that domain. IE 11为互联网站点启用了“保护模式”,当启用此功能时,即使cookie属于该域,也不会将cookie发送到我正在ping的站点。

Adding the page to the trusted sites, or disabling "Protected Mode" solved the problem. 将页面添加到受信任的站点,或禁用“保护模式”解决了这个问题。

Note that this problem does not happen when both sites are in the internet zone even when "Protected Mode" is enabled. 请注意,即使启用了“保护模式”,当两个站点都在Internet区域时也不会发生此问题。

I had similar problem and found that neither axios or jquery can be made to work with Internet Explorer and the preflight/CORS issue. 我有类似的问题,发现无论是axios还是jquery都无法使用Internet Explorer和预检/ CORS问题。 Only good old XMLhttpRequest worked. 只有好旧的XMLhttpRequest才有效。 Because in pure XMLhttpRequest we can do this: 因为在纯XMLhttpRequest中我们可以这样做:

if (xhttp.readyState == 4 && xhttp.status == 200)

It seems that axios and jquery take into account the status and not the readyState - they somehow interpret that there is a cors problem - and it takes a few ticks to reach readyState == 4 in IE. 似乎axios和jquery考虑了状态而不是readyState - 它们以某种方式解释存在cors问题 - 并且在IE中需要几个滴答来达到readyState == 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM