如何以编程方式删除Azure Active Directory用户?

时间:2015-03-16 18:45:02

标签: c# .net azure azure-active-directory

我正在使用以下代码。 代码执行正常,但不会删除用户。

IUser deleteuser = new User();
deleteuser = users.Where(myuser => myuse

r.MailNickname.Equals("IshanArora")).FirstOrDefault();

if (deleteuser != null)
{
    deleteuser.DeleteAsync();
}

1 个答案:

答案 0 :(得分:1)

我在删除用户之前通过检索删除用户:

private async Task DeleteUser()
{
    ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();

    // Retrieve user from its displayname
    var user = await client.Users.Where(u => u.DisplayName == "user two").ExecuteSingleAsync();

    // Or Retrieve user from its principal name
    // var user = await client.Users.Where(u => u.UserPrincipalName== "user two").ExecuteSingleAsync();

    // Or Retrieve user from its id in the directory
    //var user = await client.Users.GetByObjectId("0b34c995-0970-4776-ae87-f66d384f9c45").ExecuteAsync();

    // Delete the user
    await user.DeleteAsync();
}

您应该注意到,在看到用户从Azure门户中删除之前可能需要几秒钟