我正在尝试通过发送发布请求来获取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)
来源 localhost:24310 在Access-Control-Allow-Origin标头中找不到
Origin localhost:24310 not found in Access-Control-Allow-Origin header
function CallAAD() { var settings = { "async": true, "crossDomain": true, "url": "login.microsoftonline/saurabhpersonalad.onmicrosoft/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": "saurabhpersonalad.onmicrosoft/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令牌,建议您使用 azure-activedirectory-library-for-js (这是javascript中的库),用于前端轻松集成AAD.您可以参考否'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
发布评论