Azure AD / Microsoft Graph 令牌 - 用于多客户端应用程序的内容

Azure AD / Microsoft Graph Tokens - What to use for a multi-client app

我需要能够通过后端在离线模式下监控用户的 Hotmail 或 Outlook 帐户。但是用户可以通过网络应用程序注册并授权帐户访问,例如Laravel 或 Lumen,或来自 Cordova 移动应用程序或其他 SPA 界面,例如 Angular。基本上,该应用程序在 https://apps.dev.microsoft.com 上配置为隐式流。

由于该应用程序需要后端离线处理,所以一天几次 - 我需要一个刷新令牌来更新 access_token。有两种方法可以获得 Azure AD 的同意。

  1. authorize = id_token + token(但限制是 id_token 仅特定于客户端)。这种方法更适合在客户端运行且用户在线时获取电子邮件。

  2. 授权=代码然后生成access_token和refresh_token。

问题 - 选项 2 对 hotmail/outlook.com 和 O365 都有效吗?如果访问和刷新令牌是由客户端生成的——它们是否适用于用户帐户和电子邮件的在线和离线访问。

当然可以。但是如果你想在令牌响应中接收刷新令牌,你的应用程序必须请求并被授予 offline_acesss scope

The offline_access scope gives your app access to resources on behalf of the user for an extended time. On the work account consent page, this scope appears as the "Access your data anytime" permission. On the personal Microsoft account consent page, it appears as the "Access your info anytime" permission. When a user approves the offline_access scope, your app can receive refresh tokens from the v2.0 token endpoint. Refresh tokens are long-lived. Your app can get new access tokens as older ones expire.

请求示例:

// 换行只是为了易读性

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&response_mode=query
&scope=openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.read
&state=12345

实际上,如果您使用代码授权流程登录 AAD,您将看到此页面:

如果单击“是”,您将同意 offline_access 范围。注意:这适用于 MSA 和 AAD 帐户。

您可以在 this documentation 中查看有关 offline_access sope 的更多详细信息。