C# CS1061“.Request”错误(尝试通过外部应用程序更新 Azure AD B2C 中的用户)

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

我一直在努力解决“.Request()”的 CS1061 错误,似乎无法弄清楚,如果有人能想到解决方案或解决方法,我们将不胜感激!

该程序正在运行 .NET 8 并使用 Microsoft.graph、microsoft.graph.core 和 Azure.Identity 包,全部都是最新的。

 static void UpdateUser()
 {
     var scopes = new[] { "https://graph.microsoft.com/.default" };

     var tenantId = "...";


     var clientId = "...";
     var clientSecret = "...";

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

     Console.WriteLine("Updating a user...");

     Console.Write("Enter the Object ID of the user to update: ");
     string objectId = Console.ReadLine();

     try
     {

         var user = graphClient.Users[objectId].GetAsync().Result;

         // Display current user attributes
         Console.WriteLine($"Current attributes of the user with Object ID {objectId}:");
         Console.WriteLine($"Display Name: {user.DisplayName}");
         Console.WriteLine($"Given Name: {user.GivenName}");
         Console.WriteLine($"Surname: {user.Surname}");
         Console.WriteLine($"Email Address: {user.Identities[0].IssuerAssignedId}"); 
         Console.WriteLine($"Business Phone Number: {user.BusinessPhones?[0]}"); 

         // Prompt the user to update attributes
         Console.WriteLine("Enter new values for the user's attributes (press Enter to keep current value):");

         Console.Write("New Display Name: ");
         string newDisplayName = Console.ReadLine();

         Console.Write("New Given Name: ");
         string newGivenName = Console.ReadLine();

         Console.Write("New Surname: ");
         string newSurname = Console.ReadLine();

         Console.Write("New Email Address: ");
         string newEmailAddress = Console.ReadLine();

         Console.Write("New Business Phone Number: ");
         string newBusinessPhone = Console.ReadLine();

         // Update user attributes
         if (!string.IsNullOrEmpty(newDisplayName))
             user.DisplayName = newDisplayName;

         if (!string.IsNullOrEmpty(newGivenName))
             user.GivenName = newGivenName;

         if (!string.IsNullOrEmpty(newSurname))
             user.Surname = newSurname;

         if (!string.IsNullOrEmpty(newEmailAddress))
             user.Identities[0].IssuerAssignedId = newEmailAddress;

         if (!string.IsNullOrEmpty(newBusinessPhone))
         {
             if (user.BusinessPhones == null)
                 user.BusinessPhones = new List<string>();

             user.BusinessPhones.Clear(); 
             user.BusinessPhones.Add(newBusinessPhone); 
         }

         // Update the user in Azure AD B2C
         graphClient.Users[objectId].Request().UpdateAsync(user).Wait();
         Console.WriteLine("User updated successfully");
     }
     catch (Exception ex)
     {
         Console.WriteLine($"Error updating user: {ex.Message}");
     }
 }

希望这些信息足够了,如果我可以提供任何其他信息可能会有所帮助,请随时询问(我对编码还很陌生,所以很有可能我错过了一些东西!)。

提前致谢。

我尝试过重建和清理程序,我还询问chatGPT是否有任何想法为什么这可能不起作用,但它无法提供修复。所有套件均已更新。

c# azure azure-ad-b2c azure-ad-graph-api
1个回答
0
投票

最初,我注册了一个应用程序并授予了

User.ReadWrite.All
应用程序类型的权限,如下所示:

enter image description here

要更新 Azure AD B2C 中的用户,您可以使用以下修改后的代码:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
using System;

class Program
{
    static void Main(string[] args)
    {
        UpdateUser().Wait();
    }

    static async Task UpdateUser()
    {
        var scopes = new[] { "https://graph.microsoft.com/.default" };

        var clientId = "appId";
        var tenantId = "tenantId";
        var clientSecret = "secret";

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        var clientSecretCredential = new ClientSecretCredential(
            tenantId, clientId, clientSecret, options);

        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        Console.WriteLine("Updating a user...");

        Console.Write("Enter the Object ID of the user to update: ");
        string objectId = Console.ReadLine();

        try
        {
            var user = await graphClient.Users[objectId].GetAsync();

            // Display current user attributes
            Console.WriteLine($"Current attributes of the user with Object ID {objectId}:");
            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]}");

            // Prompt the user to update attributes
            Console.WriteLine("Enter new values for the user's attributes (press Enter to keep current value):");

            Console.Write("New Display Name: ");
            string newDisplayName = Console.ReadLine();

            Console.Write("New Given Name: ");
            string newGivenName = Console.ReadLine();

            Console.Write("New Surname: ");
            string newSurname = Console.ReadLine();

            Console.Write("New Email Address: ");
            string newEmailAddress = Console.ReadLine();

            Console.Write("New Business Phone Number: ");
            string newBusinessPhone = Console.ReadLine();

            // Update user attributes
            if (!string.IsNullOrEmpty(newDisplayName))
                user.DisplayName = newDisplayName;

            if (!string.IsNullOrEmpty(newGivenName))
                user.GivenName = newGivenName;

            if (!string.IsNullOrEmpty(newSurname))
                user.Surname = newSurname;

            if (!string.IsNullOrEmpty(newEmailAddress))
                user.Mail = newEmailAddress;

            if (!string.IsNullOrEmpty(newBusinessPhone))
            {
                if (user.BusinessPhones == null)
                    user.BusinessPhones = new List<string>();

                user.BusinessPhones.Clear();
                user.BusinessPhones.Add(newBusinessPhone);
            }

            // Update the user in Azure AD B2C
            await graphClient.Users[objectId].PatchAsync(user);
            Console.WriteLine("User updated successfully");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error updating user: {ex.Message}");
        }
    }
}

回复:

enter image description here

参考:

升级到 v5.0 · microsoftgraph/msgraph-sdk-dotnet 更改 · GitHub

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