【发布时间】:2017-10-12 20:10:29
【问题描述】:
我在 jQuery 调用我的 azure 应用代理时收到以下错误
XMLHttpRequest 无法加载 https://azentsearchdev01-mytenant.msappproxy.net/search?text=mytext&type=json&callback=json_callback。 预检响应无效(重定向)
这就是我正在做的事情
从 mytenantsite.sharepoint.com,通过 jQuery 调用以下 URL 上的 Azure 应用程序 - https://azentsearchdev01-mytenant.msappproxy.net
作为调用的一部分,我正在使用来自 Azure AD 的身份验证令牌(访问令牌)设置授权标头
jQuery 调用失败,302 重定向到https://login.microsoftonline.com/
这是我的代码
//authorization context
var resource = 'https://azentsearchdev01-mytenant.msappproxy.net/';
var endpoint = 'https://azentsearchdev01-mytenant.msappproxy.net/search?text=mytext&type=json&callback=json_callback';
var authContext = new AuthenticationContext({
instance: 'https://login.microsoftonline.com/',
tenant: 'mytenant.onmicrosoft.com',
clientId: 'guid for client id',
postLogoutRedirectUri: window.location.origin,
cacheLocation: 'localStorage'
});
//save tokens if this is a return from AAD
authContext.handleWindowCallback();
var user = authContext.getCachedUser();
if (user) { //successfully logged in
authContext.acquireToken("https://graph.windows.net", function (error, token) {
if (error || !token) {
jQuery("#loginMessage").text('ADAL Error Occurred: ' + error);
return;
}
$.ajax({
type: 'GET',
url: endpoint,
headers: {
Accept: 'application/json',
},
beforeSend: function(xhr, settings) {
xhr.setRequestHeader('Authorization','Bearer ' + token);
}
}).done(function (data) {
jQuery("#loginMessage").text('success');
}).fail(function (err) {
jQuery("#loginMessage").text('Error calling endpoint: ' + err.statusText); **-->This is where the code lands**
});
到目前为止 -
根据我所阅读的内容,这是浏览器处理 CORS 预检重定向的当前状态的已知差距。参考link。
问题 -
是否有任何选项可以成功调用需要 cors 预检重定向的应用程序?
【问题讨论】:
-
可以通过您控制的服务器端代理完成所有这些操作
-
那将是最坏的情况。客户端根本不是一种选择吗?
-
不基于浏览器中当前的 cors 实现
-
我认为您的问题与stackoverflow.com/questions/34949492/… 的问题不同。规范更改和 Chrome 57 更改是针对如果服务器以 200 或 204 响应预检选项,然后以 30x 响应后续 GET,则 Chrome 57+ 现在将遵循重定向而不是错误。但是在您的情况下,服务器使用 302 响应 OPTIONS 请求本身。根据规范,预检不能接受对 OPTIONS 请求本身的 302 响应。因此出现错误。
-
根据代码,您正在获取资源
https://graph.windows.net的令牌。并且重定向响应似乎访问令牌不适用于服务器。你如何保护服务器,你能从 Fiddler 等其他客户端检查它吗?
标签: jquery redirect cors azure-active-directory azure-api-apps