获取Azure Active Directory令牌JavaScript

编程入门 行业动态 更新时间:2024-10-28 06:27:53
本文介绍了获取Azure Active Directory令牌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)

来源 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

本文发布于:2023-11-13 23:10:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   Active   Azure   JavaScript   Directory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!