使用Web API 2需要使用BreezeJS进行Windows身份验证的OData端点 [英] Consuming Web API 2 OData endpoint requiring Windows Authentication with BreezeJS

查看:558
本文介绍了使用Web API 2需要使用BreezeJS进行Windows身份验证的OData端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有获得Breeze(或DataJS)与需要Windows身份验证和托管在不同的服务器上的Web API 2 OData端点通信的经验吗?

Has anyone have experience getting Breeze (or DataJS for that matter) to communicate with a Web API 2 OData endpoint which requires Windows Authentication and is hosted on a different server?

我已成功配置Web API以启用CORS,我可以使用jQuery向OData端点发起跨域请求:

I have successfully configured Web API to enable CORS and I am able to initiate a cross-domain request to the OData endpoint using jQuery:

load: function () {
    $.ajax({
        type: "GET",
        dataType: 'json',
        url: "http://services.company.com/odata/GeographicalRegions",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true
    }).success(function(data) {
        $.each(data.value, function (i, c) {
            my.vm.geographicalRegions.push(new GeographicalRegion().Id(c.Id).Code(c.Code).Name(c.Name));
        });
    });
}

但是,尝试使用Breeze总是会出现以下错误集合Chrome:

However, attempting to use Breeze always ends up in the following set of errors in Chrome:


> OPTIONS http://services.company.com/odata/GeographicalRegions 401 (Unauthorized) datajs-1.1.3.js:2608
> OPTIONS http://services.company.com/odata/GeographicalRegions No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://project.company.com' is therefore not allowed access. datajs-1.1.3.js:2608
> XMLHttpRequest cannot load http://services.company.com/odata/GeographicalRegions. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://project.company.com' is therefore not allowed access. sample.html:1
> Uncaught #<Object>

我想这个问题不是由BreezeJS库而是DataJS引起的,所以我重写了代码用DataJS测试。但是,我不知道如何告诉OData.read发出请求与当前凭据...所以,我尝试下一个最好的事情(我希望在生产环境中避免):

I figured the issue was not raised by the BreezeJS library but rather DataJS, so I re-wrote the code to test with DataJS. However, I could not figure out how to tell OData.read to issue the request with the current credentials... So, I tried the next best thing (which I would hope to avoid in a Production environment):

load: function () {
    OData.read({
        requestUri: "http://services.company.com/odata/GeographicalRegions",
        user:"username", 
        password:"password"
    }, function(data) {
        $.each(data.results, function(i, c) {
            my.vm.geographicalRegions.push(new GeographicalRegion().Id(c.Id).Code(c.Code).Name(c.Name));
        });
    });
}

错误保持不变...任何有关如何解决问题的想法?我讨厌使用jQuery ...这是一个伟大的库,但我希望加快开发周期使用DataJS。

The error remains the same... Any ideas on how to overcome the issue? I'd hate to use jQuery... It is a great library but I was hoping to speed up the development cycle using DataJS.

一个MAJOR注意...解决方案必须支持IE 8由于客户的要求。

One MAJOR caveat... Solution must support IE 8 due to customer requirements. That just took JayData out of the running.

推荐答案

IE8不支持CORS 。这可能是问题吗?

IE8 does not support CORS. Could that be the problem?

您可能已经发现,标准Breeze OData dataservice适配器委派给DataJS。

As you have probably discovered, the standard Breeze OData dataservice adapter delegates to DataJS.

默认情况下,DataJS进行自己的AJAX调用,直接访问浏览器的 XMLHttpRequest ,除非 替换您自己的http客户端

By default DataJS makes its own AJAX calls, talking directly to the browser's XMLHttpRequest unless you substitute your own http client. You might be able to do follow their lead in tweaking the headers per whatever jQuery is doing under the hood.

我从来没有发现过jQuery crossDomain config值得一个修补匠的死。我spelunked入 crossDomain 代码,而且结论是它不能神奇地使浏览器CORS兼容,如果它不是CORS兼容。

I never found that jQuery crossDomain config worth a tinker's damn. I spelunked into the crossDomain code a while back and concluded that it couldn't magically make a browser CORS compatible if it isn't CORS compatible.

但是如果你可以使用jQuery工作,你可以告诉DataJS使用你自己的jQuery AJAX封装。 Breeze永远不会知道或关心它。

But if you can make things work with jQuery, you can tell DataJS to use your own jQuery AJAX wrapper. Breeze will never know or care about it.

这篇关于使用Web API 2需要使用BreezeJS进行Windows身份验证的OData端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆