在ASPNet Core 2.1中使用Microsoft Graph查询用户组

Use Microsoft Graph to query user groups in ASP Net Core 2.1

是否可以在旧的 ASP Net Core 2.1 应用程序中使用 Microsoft Graph?

我的应用程序使用 OpenID Connect 对用户进行身份验证,但我需要查询 Microsoft Graph 以获取用户组。 (以前这是使用 Azure AD Graph 完成的)

谁能给我指出一些使用 2.1 框架的旧代码示例。我找到的大多数示例都使用了新的框架版本。

// Microsoft.AspNetCore.Authentication.OpenIdConnect, Version=2.1.2.0

          .AddOpenIdConnect(option =>
          {
              option.ClientId = ClientId;
              option.ClientSecret = ClientSecret;
              option.Authority = $"https://login.microsoftonline.com/{Tenant}";
              option.Resource = "https://graph.microsoft.net";
              option.SignedOutRedirectUri = SignedOutRedirectUri;
              option.CorrelationCookie.Name = "cookie name";
              option.NonceCookie.Name = "nonce name";
              option.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;                
              option.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
              ....
          }

Microsoft.Graph 软件包的最新版本是 .NET Standard 2.0,可与 .NET Core 2.1 一起使用。

Microsoft Graph Tutorial 将带您了解最基本的知识。

根据您使用 MS Graph 的目的,有很多内容需要涵盖,但我确实找到了一小组可能有用的示例。

https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user

Note: I highly encourage you to update to the latest Long Term Support (LTS) version of .NET. .NET Core 2.1 has been End-of-Lifecycle (EOL) since Aug, 2021.

更新

我清理了一个工作示例,并在此处生成了一个能够检索用户组的小型示例应用程序。

这里是GitHub Repository

分配给应用注册的图表权限如下:

委派权限:

  • Group.Read.All
  • GroupMember.Read.All
  • offline_access
  • openid
  • 简介
  • User.Read
  • User.Read.全部
  • User.Read基本.全部