You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.Request()引发CS1061错误:Azure AD B2C用户更新问题求助

解决.NET 8中使用Microsoft Graph SDK v5更新Azure AD B2C用户时的CS1061错误

错误根源

你遇到的CS1061错误是因为最新版Microsoft Graph SDK(v5及以上)已经彻底移除了.Request()链式调用,原来的graphClient.Users[objectId].Request().UpdateAsync()写法在v5版本中不再生效。

修复方案

1. 调整更新调用语法

直接使用PatchAsync方法(SDK v5中更新资源的标准方法)替代过时的.Request().UpdateAsync(),同时注意不要直接修改从Graph获取的完整User对象(包含大量只读属性,提交会引发错误),而是创建一个仅包含需要更新字段的User实例。

2. 优化异步调用方式

避免使用.Result和.Wait()这类同步阻塞写法,改用async/await模式,这是.NET异步编程的标准实践,也能避免潜在的死锁问题。

修复后的完整代码

static async Task 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 existingUser = await graphClient.Users[objectId].GetAsync();

        // 展示当前用户属性
        Console.WriteLine($"Current attributes of the user with Object ID {objectId}:");
        Console.WriteLine($"Display Name: {existingUser.DisplayName}");
        Console.WriteLine($"Given Name: {existingUser.GivenName}");
        Console.WriteLine($"Surname: {existingUser.Surname}");
        Console.WriteLine($"Email Address: {existingUser.Identities?[0].IssuerAssignedId}");
        Console.WriteLine($"Business Phone Number: {existingUser.BusinessPhones?[0]}");

        // 提示输入新属性值
        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();

        // 创建仅包含需要更新字段的User对象
        var userToUpdate = new User();

        if (!string.IsNullOrEmpty(newDisplayName))
            userToUpdate.DisplayName = newDisplayName;

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

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

        // 更新邮箱:注意Azure AD B2C中Identities的格式要求
        if (!string.IsNullOrEmpty(newEmailAddress) && existingUser.Identities?.Count > 0)
        {
            userToUpdate.Identities = new List<ObjectIdentity>
            {
                new ObjectIdentity
                {
                    Issuer = existingUser.Identities[0].Issuer,
                    IssuerAssignedId = newEmailAddress,
                    SignInType = existingUser.Identities[0].SignInType
                }
            };
        }

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

        // 执行更新操作
        await graphClient.Users[objectId].PatchAsync(userToUpdate);
        Console.WriteLine("User updated successfully");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error updating user: {ex.Message}");
        // 如需更详细错误信息,可打印内部异常
        // Console.WriteLine($"Detailed error: {ex.InnerException?.Message}");
    }
}

额外注意事项

  1. NuGet包版本确认:确保你安装的是Microsoft.Graph v5.x版本,以及匹配版本的Azure.Identity和Microsoft.Graph.Core。
  2. 权限配置:你的应用程序需要拥有User.ReadWrite.All或Directory.ReadWrite.All的应用权限(已授予管理员同意),否则会出现权限不足的错误。
  3. Identities更新限制:在Azure AD B2C中,直接修改用户的Identities需要确保格式正确,必须包含Issuer、IssuerAssignedId和SignInType三个字段,且Issuer必须与租户的B2C域名匹配。

内容的提问来源于stack exchange,提问作者Prime Capybara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:27:37