获取Azure活动目录令牌javascript

时间:2016-12-05 14:15:21

标签: javascript jquery azure azure-active-directory

我试图通过发送帖子请求来获取AAD oauth 2.0令牌,但总是收到以下错误。(请注意在C#中使用类似的代码可以很好地工作,也可以使用fiddler / postman)

在Access-Control-Allow-Origin标头中找不到原始http://localhost:24310

  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);
            });

1 个答案:

答案 0 :(得分:1)

client_credentials grant_type,因为该grant_type适用于可以保护客户机密码安全的机密客户端,而jQuery适用于基于浏览器的公共客户端。您可能希望使用Web应用程序的后端使用客户端凭据调用Web api。

但是,要在broswer客户端上获取AAD oauth 2.0令牌,我们建议您使用azure-activedirectory-library-for-js这是javascript中用于前端的库,以便轻松集成AAD。有关详细信息,请参阅No 'Access-Control-Allow-Origin' header with Microsoft Online Auth

相关问题