Wriju's BLOG

.NET, Cloud and everything

How to delete Azure AD User using Graph API

Adding user to Azure AD (AAD) is straight forward. However when we want to delete the user we may face some issues. I have discussed that in my previous post “Error Authorization_RequestDenied while Editing Azure AD Objecthttps://blogs.msdn.microsoft.com/wriju/2016/07/20/error-authorization_requestdenied-while-editing-azure-ad-object/

Once you have the Application added to AAD and using that to manipulate the objects you may use this code below,

private
ActiveDirectoryClient GetAADClient()

{


Uri serviceRoot = new
Uri(serviceRootURL);


ActiveDirectoryClient adClient = new
ActiveDirectoryClient(

serviceRoot,


async () => await GetAppTokenAsync());


return adClient;

}

public
async
Task DeleteUser(string userGuid)

{


var adClient = GetAADClient();


var deletedUser =


await adClient.Users.Where(usr => usr.ObjectId.Equals(userGuid))

.ExecuteSingleAsync();

 


try

{


await deletedUser.DeleteAsync();

}


catch

{

}

}

This should help you delete.

Namoskar!!!