Delete user in active directory using c#

前端 未结 2 1444
感动是毒
感动是毒 2020-12-31 16:39

I\'ve written some code but not works it throws Exception \"An operations error occurred.\" code --->

DirectoryEntry dirEntry = new DirectoryEntry(\"LDAP pat         


        
相关标签:
2条回答
  • 2020-12-31 17:18

    If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on System.DirectoryServices.AccountManagement

    Basically, you can define a domain context 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 really easy to play around with users and groups in AD!

    0 讨论(0)
  • 2020-12-31 17:19

    When you are already using a 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 讨论(0)
提交回复
热议问题