...">

Delete user in active directory using C #

I wrote some code, but it doesn’t work, it throws an exception "Operational error occurred". code --->

DirectoryEntry dirEntry = new DirectoryEntry("LDAP path", "admin-username", "admin-password"); dirEntry.Properties["member"].Remove("username-delete"); dirEntry.CommitChanges(); dirEntry.Close(); 

give me some ideas to get rid of this.

+6
source share
2 answers

If you are using .NET 3.5 and above, you should check the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read more here:

Basically, you can define the context of a domain and easily find users and / or groups in AD:

 // set up domain context PrincipalContext ctx = new PrincipalContext(ContextType.Domain); // find the user you want to delete UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); if(user != null) { user.Delete(); } 

The new S.DS.AM makes it very easy to play with users and groups in AD!

+9
source

When you are already using DirectoryEntry, there is no need for PrincipalContext or UserPrincipal.

You can simply use the DeleteTree() method:

 DirectoryEntry dirEntry = new DirectoryEntry("LDAP path", "admin-username", "admin-password"); dirEntry.DeleteTree(); 
0
source

Source: https://habr.com/ru/post/909254/


All Articles