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.
If you are using .NET 3.5 and above, you should check the System.DirectoryServices.AccountManagement
(S.DS.AM) namespace. Read more here:
- Managing directory security principles in the .NET Framework 3.5
- MSDN docs in System.DirectoryServices.AccountManagement
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!