简体   繁体   English

如何获取已登录用户的Azure AD访问令牌?

[英]How to get Azure AD access token for already logged in user?

We have an App Service (a PHP website) running in our Microsoft Azure platform. 我们在Microsoft Azure平台中运行一个应用程序服务(一个PHP网站)。 The website is only accessible after the user has already been logged in. The authentication is based on the Azure AD. 只有在用户已经登录后,才能访问该网站。身份验证基于Azure AD。 Everything works fine. 一切正常。

Now, from the PHP code of the website, I would like to get the Azure AD group memberships of the already logged in user. 现在,从网站的PHP代码中,我想获取已登录用户的Azure AD组成员身份。

I read a lot of Web resources explaining how to log in a user to Azure AD from a website and thus get an authorization/access token in order to perform additional actions, like retrieving group memberships and a lot of other stuff. 我阅读了很多Web资源,解释了如何从网站登录用户到Azure AD,从而获得授权/访问令牌,以执行其他操作,例如检索组成员身份和许多其他内容。 But this is not what I'm looking for. 但这不是我想要的。 The user already is logged in and can successfully use the website. 用户已经登录并且可以成功使用该网站。 (Microsoft would redirect the user to the single sign-on login page otherwise.) (否则,Microsoft会将用户重定向到单点登录页面。)

Using PHP, I can retrieve, for example, the username of the logged in user from the request header of any access to the website. 使用PHP,例如,我可以从对网站的任何访问的请求标头中检索登录用户的用户名。 And there is also some "access token" stored in the request header, and other things, like "client principal ID". 并且在请求标头中还存储了一些“访问令牌”,以及其他内容,例如“客户端主体ID”。 But I don't know whether this is such authorization/access token I could use for additional actions. 但是我不知道这是否是我可以用于其他操作的授权/访问令牌。 Or maybe/probably I'm doing something wrong. 也许/也许我做错了什么。 Requesting, for example, https://graph.microsoft.com/v1.0/me/memberOf using cURL, I get an InvalidAuthenticationToken error with "CompactToken parsing failed". 例如,使用cURL请求https://graph.microsoft.com/v1.0/me/memberOf ,我收到一个InvalidAuthenticationToken错误,提示“ CompactToken解析失败”。 I put "Authorization:Bearer {access token}" and "Accept:application/json" into the request header for this as explained here . 我把“授权:承载{访问令牌}”和“接受:应用/ JSON”到请求头中将此作为解释在这里 (I don't have explicit information about the token type so I just assume "Bearer" to be correct like in the examples. Maybe this is already wrong.) (我没有关于令牌类型的明确信息,因此我只像示例中那样假设“承载者”是正确的。也许这已经是错误的。)

I don't know how to proceed in order to get to a solution. 我不知道如何进行解决。 Maybe the point I am missing is not in my cURL request at all but in the Azure settings for the AD and/or App Service. 也许我遗漏的一点根本不在我的cURL请求中,而是在AD和/或App Service的Azure设置中。 I'm hoping for help from your side. 我希望您能提供帮助。

You are on the right track. 您走在正确的轨道上。

Access token 访问令牌

When you have successfully authenticated yourself against Azure AD, using OAuth2 or OIDC , you will get an access token . 使用OAuth2或OIDC针对Azure AD成功进行身份验证后,您将获得访问令牌 The access token is a base 64 encoded JSON Web Token (JWT) and can be used to access other protected resources. 访问令牌是基于64位编码的JSON Web令牌(JWT),可用于访问其他受保护的资源。

An access token might look like this: 访问令牌可能看起来像这样:

EwAoA8l6BAAU ... 7PqHGsykYj7A0XqHCjbKKgWSkcAg==

You can use https://jwt.io to explore further its contents. 您可以使用https://jwt.io进一步浏览其内容。

Note: the above token is shortened. 注意:以上令牌已缩短。 In its complete form, it is quite long. 就其完整形式而言,它相当长。

In other works, you need the access token for doing future requests against any API secured with the same identity provider (in this case Azure AD). 在其他工作中,您需要访问令牌来针对将来使用相同身份提供者(在本示例中为Azure AD)保护的任何API发出请求。

Accessing protected APIs 访问受保护的API

However, before you can use your token to access any API you must first grant your Azure AD application necessary permissions. 但是,在使用令牌访问任何API之前,必须首先授予Azure AD应用程序必要的权限。 See this link . 看到这个链接

Note: that link applies to the v1.0 Azure AD endpoints. 注意:该链接适用于v1.0 Azure AD终结点。

Once you got the needed permissions sorted, you can start doing requests. 排序所需的权限后,就可以开始执行请求。 The requests you make look like, eg 您发出的请求看起来像,例如

GET https://graph.microsoft.com/v1.0/me 
Authorization: Bearer eyJ0eXAiO ... 0X2tnSQLEANnSPHY0gKcgw
Host: graph.microsoft.com

Here, the token is what comes after Authorization: Bearer . 在这里,令牌是在Authorization: Bearer

Further reading 进一步阅读

See this link for further reading. 请参阅此链接以进一步阅读。 The link contains intructions on how to use the v2.0 Azure AD endpoints but the general idea is the same: 该链接包含有关如何使用v2.0 Azure AD终结点的说明,但总体思路是相同的:

  1. Register an app 注册一个应用
  2. Grant permissions to app to access resources 授予应用访问资源的权限
  3. Get an access token 获取访问令牌
  4. Use the token for doing requests 使用令牌执行请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM