Delete user in active directory using c#

前端 未结 2 1543
感动是毒
感动是毒 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!

提交回复
热议问题