调用 Microsoft Graph 时选择正确的 Azure AD 身份验证版本

Choosing the right Azure AD auth version when calling Microsoft Graph

我是 Microsoft Graph API 和 Azure 的新手。我想寻求有关我应该使用哪个 Microsoft Graph API 版本以及我是否应该在我的场景中使用 "Web API on-behalf-of flow" 的建议。

我正在构建一个 Web 服务,它可以存储来自不同组织的多个 Office 365 用户的访问令牌。然后,此 Web 服务可以通过 Microsoft Graph API 创建 Web 挂钩,以获取有关这些用户帐户中日历约会更改的通知,以便将这些更改同步到存储在我们自己的服务器上的相应约会。

所以这是一个大量的 Office 365 日历同步 Web 服务 shell。

我已经完成了很多他们的 GitHub 示例项目,并设法使用 v1 图形订阅创建了网络挂钩 API 并且能够与我的开发帐户的日历进行交互,所有在示例 APS.NET MVC 项目中。

但是我对以下部分很困惑:

  1. 因为此 Web 服务不直接提供 UI,所以登录 UI 将由单独的桌面 (WPF) 客户端呈现,我相信在客户端完成后一边,我可以将经过身份验证的访问令牌转发到我的网络服务以创建网络挂钩吗?这听起来像 Microsoft 在此处描述的 "Web API on-behalf-of flow" 场景:https://docs.microsoft.com/en-au/azure/active-directory/develop/active-directory-v2-limitations.
  2. 因为此 Web 服务需要为来自不同组织的多个 Office 365 帐户创建 Web 挂钩。我不确定这是否算作多租户场景。如果是这种情况,看起来我只能使用 v1 API 因为 v2 API 只允许 Web 服务从具有相同应用程序 ID 的应用程序接收令牌(也在上面链接的页面)。

Microsoft Graph 和 Azure AD 开发人员能否为我说明一下这部分内容? Microsoft 在记录这些部分方面做得不是最好。

Because this web service does not directly provide a UI, so the login UI would be presented by a separate desktop (WPF) client, and I believe when this is done on the client side, I can forward the authenticated access token to my web service to create the web hooks? This sounds like the "Web API on-behalf-of flow" scenario Microsoft described here: https://docs.microsoft.com/en-au/azure/active-directory/develop/active-directory-v2-limitations.

是的,该场景是代流,目前v2.0端点不支持该流。

Because this web service needs to create web hooks to multiple Office 365 accounts from different organisations. I'm not sure if this counts as the a multi-tenant scenario. If this is the case, it looks like I can only use the v1 API because the v2 API only allows the web service to receive tokens from an application that has the same application ID (also described in the page linked above).

您只能使用 Azure AD V1 终结点,因为 V2.0 终结点不支持代流。以下是使用 V1 端点的一些步骤供您参考:

  1. 注册 2 个应用程序,一个用于 WPF(本机应用程序),一个用于您的网络服务(网络应用程序)
  2. 为 Web 服务的应用程序启用多租户
  3. 授予 Web 应用相关的 Microsoft Graph 权限
  4. 使用本机应用程序的 clientId 为 Web 应用程序设置 knownClientApplications
  5. 将相关的 Microsoft Graph 权限和 Web 应用授予本机应用

之后,当不同租户的用户首次登录WPF时,用户可以同时同意两个应用程序。然后将两个应用程序的服务主体注册到用户的租户。之后,Web 服务可以使用代表流根据本机应用程序的令牌获取 Microsoft Graph access_token。

更多关于多租户开发的细节,请参考下面:

How to sign in any Azure Active Directory (AD) user using the multi-tenant application pattern

下面的代码示例也很有帮助:

Calling a downstream web API from a web API using Azure AD