【发布时间】:2019-08-21 17:57:03
【问题描述】:
我目前有一个 Wep 应用程序 (ASP.NET Core 2.2),并且一个 Azure 函数返回 JSON“hi”。没有使用 NodeJS。
我已经使用 B2C 设置了我的 Web 应用程序和功能,当单独访问它们时,它们都提示登录并且工作正常。
更新:如果我访问 Web App,登录然后访问函数 url,它会给出响应“hi”但 Ajax 请求将无法工作,除非我手动访问函数 url 地址通过 Web App 登录后手动操作。
我的问题是我从网络应用程序向函数发出了 Ajax 请求,但它没有传递凭据,并且只有在我同时登录网络应用程序和函数时才有效。
我希望帮助让网络应用程序将其凭据传递给函数,因此我不会收到以下错误:
访问 XMLHttpRequest 在 'https://xxx.b2clogin.com/xxx.onmicrosoft.com/oauth2/v2.0/authorize?response_type=id_token&redirect_uri=https%3A%2F%2Fxxx.azurewebsites.net%2F.auth%2Flogin%2Faad%2Fcallback&client_id=5e55f7e5-2b51-4a5f-86c0-b7776b2623b2&scope=openid+profile+email&response_mode=form_post&p=b2c_1_signin&nonce=d138edf541d1423e92b99fe7ca400263_20190331110324&state=redir%3D%252Fapi%252FTest%253F_%253D1554029904731' (重定向自 'https://xxx.azurewebsites.net/api/Test?_=1554029904731') 来自原点 “null”已被 CORS 策略阻止:否 请求中存在“Access-Control-Allow-Origin”标头 资源。
function Test2() {
console.log("2. Sending request...");
$.ajax({
type: "GET",
cache: false,
credentials: 'include',
mode: 'cors',
url: 'https://xxx.azurewebsites.net/api/Test',
// contentType: "application/json; charset=utf-8",
//dataType: "application/json",
//data: dataValue,
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (response) {
console.log('2. Done');
console.log('2. ' + response);
},
failure: function (response) {
console.log("2. Request: Failed.");
alert(response.d);
}
});
}
【问题讨论】:
-
嗨,利亚姆。您使用什么 Auth 库来验证您的用户? msal.js / adal.js?
-
从 ADAL 的外观来看,因为我的“Microsoft.IdentityModel.Clients.ActiveDirectory”包处于停滞状态。我目前没有在任何视图中引用 msal.js 或 adal.js。
-
Jackson:认证由平台负责(它基于 cookie)。就像利亚姆提到的那样,单独地,它有效,但如果结合起来,则无效。似乎没有任何方法可以自动将身份验证上下文从 Web 应用程序传递到函数。
标签: javascript azure asp.net-core azure-functions azure-ad-b2c