在旧应用程序中,我公司的一些人无需登录用户即可从 Microsoft Graph 获取信息。我尝试复制此操作,但在尝试获取用户时获得未经授权。我认为图表可能已更改,或者当我注册应用程序时,我在 Azure 中执行了错误的操作。
因此,在 Azure 门户中,我注册了一个应用程序(Web 应用程序),并授予其 Azure ad 和 Microsoft graph 的权限,以读取所有用户的完整个人资料。
然后我提出一个请求
var client = new RestClient(string.Format("https://login.microsoftonline.com/{0}/oauth2/token", _tenant));
var request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("tenant", _tenant);
request.AddParameter("client_id", _clientId);
request.AddParameter("client_secret", _secret);
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("resource", "https://graph.microsoft.com");
request.AddParameter("scope", "Directory.Read.All");
我在测试时添加了最后一行(范围)。没有这个我仍然得到了一个 token ,但无论有没有它,结果都是一样的。
获得 token 后,我将其保存并执行此请求:
var testClient = new RestClient(string.Format("https://graph.microsoft.com/v1.0/users/{0}", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b6a7b1b682b6a7b1b6ecadacafaba1b0adb1ada4b6eca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>")); //I use a real user here in my code ofc.
testRequest = new RestRequest();
testRequest.Method = Method.GET;
testRequest.AddParameter("Authorization", _token.Token);
var testResponse = testClient.Execute(testRequest);
但是现在我收到一条错误消息,指出未经授权,承载访问 token 为空。 这些错误提示我登录用户并执行请求,但是我不想登录用户。据我所知,这以前是可能的。 Microsoft 是否已将其更改为不允许匿名请求?
如果是这样,是否可以不将用户重定向到同意页面?用户已通过 Owin 登录。但是,用户可能有不同的访问权限,我希望这个应用程序能够访问 azure 广告中的所有内容,无论哪个用户登录。现在执行此操作的正确方法是什么?
或者我只是错过了一些明显的东西?该应用已获得对 azure 和 Microsoft graph 的访问权限,并且管理员已授予该应用的权限。
编辑:为了澄清,我尝试了“Authorization”、“bearer”+ _token.Token,以及片段中的 _token.Token。
最佳答案
是的,仍然可以在没有用户在场的情况下使用 application permissions 向 Graph 发出请求。 。您需要获得租户管理员的同意并批准您的申请。
编辑/回答:将“授权”添加为标题而不是参数就可以了。它既适用于“承载 token ”,也适用于“ token ”
关于azure - 匿名询问用户信息 Microsoft Graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191744/