获取Azure Active Directory令牌JavaScript [英] Getting Azure active directory token javascript
问题描述
我正在尝试通过发送发布请求来获取AAD oauth 2.0令牌,但始终会收到以下错误.(请注意,在C#中使用类似的代码非常有效,并且也可以使用提琴手/邮递员)
I am trying to get AAD oauth 2.0 token by sending a post request but always getting the following error.(Please note using similar code in C# works perfectly and also using fiddler/postman)
来源 http://localhost:24310 在Access-Control-Allow-Origin标头中找不到
Origin http://localhost:24310 not found in Access-Control-Allow-Origin header
function CallAAD()
{
var settings = {
"async": true,
"crossDomain": true,
"url": "https://login.microsoftonline.com/saurabhpersonalad.onmicrosoft.com/oauth2/token",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"Access-Control-Allow-Origin":"*"
},
"data": {
"grant_type": "client_credentials",
"client_id": "18cff243-e5f1-4e6e-9432-1790724eeb50",
"client_secret": "aUoWP9tNSDXblVvn/blmFkJtGyo8HM+YIb4JeIipdL8=",
"resource": "https://saurabhpersonalad.onmicrosoft.com/WebApplication6"
}
}
$.support.cors = true;
$.ajax(settings).done(function (response) {
debugger;
alert(response);
});
推荐答案
client_credentials
grant_type,因为grant_type适用于可以保护客户端机密安全的机密客户端,而jQuery则适用于基于浏览器的公共客户端.您可能想使用Web应用程序的后端使用客户端凭据来调用Web api.
client_credentials
grant_type as that grant_type is for confidential clients who can keep the client secrets secure whereas jQuery is meant for browser based public clients. You might want to use your web app's backend to make calls to the web api with the client credentials.
但是,要在浏览器客户端上获得AAD oauth 2.0令牌,建议您使用否'Access-带有Microsoft Online Auth的Control-Allow-Origin标头以获取详细信息.
However, to get AAD oauth 2.0 token on broswer clients, we suggest you to use azure-activedirectory-library-for-js which is a library in javascript for frontend to integrate AAD with a ease. You can refer to No 'Access-Control-Allow-Origin' header with Microsoft Online Auth for details.
这篇关于获取Azure Active Directory令牌JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!