.Request() 无法在 C# 中与 Azure AD B2C 配合使用

问题描述 投票:0回答:1

我有一个用 C# 编写的管理应用程序,扩展名为

Microsoft.Graph.Beta
,它使用 .Request() 来允许查看和搜索 B2C 中的用户,我有一个单独的应用程序,它使用新的
5.52.0 Microsoft Graph
Azure.Identity
可用于使用
Users.PostAsync
方法在 B2C 中添加和更新用户。由于这是更安全的应用程序,我需要迁移第一个应用程序的功能,但新的 Graph 似乎使 .Request() 变得多余,这是我所知道的从 B2C 获取数据的唯一方法。

这是第一个应用程序的查看代码 -

 public static async Task ListUsers(GraphServiceClient graphClient)
 {
     Console.WriteLine("Getting list of users...");

     // Get all users (one page)
     var result = await graphClient.Users
         .Request()
         .Select(e => new
         {
             e.DisplayName,
             e.Id,
             e.Identities,
             e.BusinessPhones
         })
         .GetAsync();

     // Calculate maximum lengths
     int maxDisplayNameLength = result.CurrentPage.Max(u => u.DisplayName.Length);
     int maxIdLength = result.CurrentPage.Max(u => u.Id.Length);
     int maxEmailLength = result.CurrentPage
         .Where(u => u.Identities.Any(i => i.SignInType == "emailAddress")) // Filter users with email addresses
         .Select(u => u.Identities.FirstOrDefault(i => i.SignInType == "emailAddress")?.IssuerAssignedId)
         .Where(email => email != null)
         .Max(email => email.Length);

     bool isFirst = true;

     foreach (var user in result.CurrentPage)
     {
         // Get email if available
         string email = user.Identities.FirstOrDefault(i => i.SignInType == "emailAddress")?.IssuerAssignedId;

         // If user has no email, skip to the next iteration
         if (email == null)
             continue;

         // Get phone numbers
         string phoneNumbers = string.Join(", ", user.BusinessPhones);

         if (isFirst)
         {
             Console.WriteLine("------------------------");
             isFirst = false;
         }

         // Format output with alignment based on maximum lengths
         Console.WriteLine($"DisplayName: {user.DisplayName.PadRight(maxDisplayNameLength)}");
         Console.WriteLine($"Id: {user.Id.PadRight(maxIdLength)}");
         Console.WriteLine($"Email: {email?.PadRight(maxEmailLength) ?? string.Empty}");
         Console.WriteLine($"Phone Numbers: {phoneNumbers}"); // Display phone numbers
         Console.WriteLine("------------------------"); // Add a line separator
     }
 }

它在旧的 Graph 版本中完美运行,但出于安全原因我需要将其迁移到新应用程序中。下面是在新应用程序中尝试相同的逻辑,如果运行,您会注意到它会抛出与 .Request() 相关的错误 -

`static async Task ListUsers()
        {
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var tenantId = "...";
            var clientId = "...";
            var clientSecret = "...";

            var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            try
            {
                // Retrieve users from Microsoft Graph
                var users = await graphClient.Users.Request().GetAsync();

                // Display user details
                foreach (var user in users)
                {
                    Console.WriteLine($"Display Name: {user.DisplayName}");
                    Console.WriteLine($"Given Name: {user.GivenName}");
                    Console.WriteLine($"Surname: {user.Surname}");
                    Console.WriteLine($"Email Address: {user.Mail}");
                    Console.WriteLine($"Business Phone Number: {user.BusinessPhones?[0]}");
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error listing users: {ex.Message}");
            }
        }`

现代Graph中有没有新的方法来替代这个?

c# azure-ad-b2c visual-studio-2022
1个回答
0
投票

注意

Request()
已从最新版本的 Microsoft Graph 中删除,因此您需要从代码中删除
Request()
才能解决该错误。请参阅此GitHub 博客

要解决该错误,请修改如下代码:

using Microsoft.Graph;
using Azure.Identity;

class Program
{
    static async Task Main()
    {
        string tenantId = "TenantID";
        string clientId = "ClientID";
        string clientSecret = "ClientSecret";
        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);

        var graphClient = new GraphServiceClient(clientSecretCredential);

        try
        {

            var users = await graphClient.Users.GetAsync((requestConfiguration) =>
            {
                requestConfiguration.QueryParameters.Select = new string[] { "displayName", "GivenName", "Surname", "Mail" };
            });

            foreach (var user in users.Value)
            {
                Console.WriteLine($"Display Name: {user.DisplayName}");
                Console.WriteLine($"Given Name: {user.GivenName}");
                Console.WriteLine($"Surname: {user.Surname}");
                Console.WriteLine($"Street Address: {user.Mail}");
                Console.WriteLine(" ");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error listing users: {ex.Message}");
        }
    }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.