在本地使用目录同步时更新 Azure AD 中的属性

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

我们有一个包含多个本地域的设置,它们全部同步到一个公共的 Azure AD。

现在的问题是,我们有一个领域的人员是其他领域人员的管理者。

尝试使用我的代码在 Azure AD 中设置管理器属性时,出现以下错误:

无法更新本地控制的目录同步对象或当前正在进行迁移的对象的指定属性

我尝试这样更新

public async Task<bool> UpdateManagerForUser(string userId, string managerId)
        {   
            try
            {
                await _graphServiceClient.Users[userId].Manager.Reference.Request().PutAsync(managerId);

                return true;
            } catch(Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

可以不做吗?或者有什么解决办法吗?

提前致谢

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

如果您尝试更新通过 Azure AD 直接连接到 Azure AD 云从本地域同步的用户属性,通常会出现此错误。

我将以下用户从本地域同步到我的 Azure AD 租户:

enter image description here

当我运行下面的示例代码来指定

admanager
作为
aduser1
的经理时,我也遇到了 与您相同的错误

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var requestBody = new Microsoft.Graph.Models.ReferenceUpdate
{
    OdataId = "https://graph.microsoft.com/v1.0/users/<admanagerID>",
};

try
{
    await graphClient.Users["aduser1Id"].Manager.Ref.PutAsync(requestBody);
    Console.WriteLine("Successfully added manager");
}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
}

回复:

enter image description here

请注意,不可能直接通过 Portal 或 Graph API 更新本地同步用户的属性。

相反,您需要先更新

on-premises
Active Directory 中的 manager 属性,然后通过 Azure AD Connect 将这些更改同步到 Azure AD。

就我而言,我在 on-premises

 Active Directory 中将 
admanager
 添加为 
aduser1 的经理,如下所示:

enter image description here

通过 Azure AD 连接完成同步后,

aduser1
会在门户中成功更新为 Manager 属性:

enter image description here

您还可以使用以下 Microsoft Graph API 查询检索更新的 manager 属性:

GET https://graph.microsoft.com/v1.0/users/[email protected]/manager

回复:

enter image description here

示例 C# 代码:

using Azure.Identity;
using Microsoft.Graph;

var scopes = new[] { "https://graph.microsoft.com/.default" };

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

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

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

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var result = await graphClient.Users["[email protected]"].Manager.GetAsync();

Console.WriteLine(result.Id);

回复:

enter image description here

如果您希望通过 PowerShell 将一个域中的用户作为 manager 添加到 on-premises Active Directory 中另一个域中的用户,请检查 @Santiago Squarzon 的这个 SO 线程

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