如何使用 Microsoft.Azure.ActiveDirectory.GraphClient 在 AzureAD 中删除用户管理器

How to remove a users manager in AzureAD using Microsoft.Azure.ActiveDirectory.GraphClient

我正在使用 Microsoft.Azure.ActiveDirectory.GraphClient(版本 2.1.0)编写用于 Azure AD 用户管理的应用程序。我可以设置用户的管理员,但不知道如何清除该字段。

很遗憾,GitHub 上提供的示例项目也不包含此功能。

//Assign and remove user's manager
// User.Manager = newUser as DirectoryObject;
           User.Manager = null;

我设法使用下面的代码清除了 "manager" 字段。它没有使用 Microsoft.Azure.ActiveDirectory.GraphClient 库,但可以完成工作。

var token = <get your adal token here>
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Bearer", token);

var url = "https://graph.windows.net/<tenant domain>/users/<userid>/$links/manager?api-version=1.6"
var resp = httpClient.DeleteAsync(url).Result;
if (!resp.IsSuccessStatusCode)
{
    // log / throw exception etc.   
}

您需要向 https://graph.microsoft.com/v1.0/users/<user_email>/manager/$ref 执行 DELETE HTTP 请求(确保替换 URL 中的 <user_email>

成功调用将收到204响应代码和空字符串作为响应正文。

Microsoft Graph API 文档目前缺少此方法,但应在将来添加。 (参见 here

您还应该开始使用 Microsoft Graph (graph.microsoft.com) 而不是 Azure AD Graph (graph.windows.net),因为后者已经过时了。 (参见 here