Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Add Active Directory User to Local Group

Add Active Directory User to Local Group

Scheduled Pinned Locked Moved C#
windows-adminhelpquestionworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    svh1985
    wrote on last edited by
    #1

    Hello! I have some code that add's users to my local administrator group. But what i would like to accoplish is to add a user from an active directory to my localmachine admin group. My code can add a local user to the local admin group. I tried to alter the LDAP string for the ActiveDirectory connection but now it fails to add the domain user to the localmachine admin group. The AD LDAP String: ldap://SERVERNAME01:389/CN=TESTUSER01,CN=Users,DC=DOMAIN01,DC=local The Test String: WinNT://WORKGROUP/STEPHAN-F894E19/TestUser1 The Test String works the AD LDAP String doesn't work. Can anyone help me with this? Thanks! the Code:

       private void AddUserToGroup() 
        { 
            try 
            { 
                DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
                DirectoryEntry grp; 
    
                grp = AD.Children.Find("Administrators", "group"); 
                if (grp != null) { grp.Invoke("Add", new object\[\] { "WinNT://WORKGROUP/STEPHAN-F894E19/TestUser1" }); } 
                MessageBox.Show("Account Created Successfully"); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        }
    
    S 1 Reply Last reply
    0
    • S svh1985

      Hello! I have some code that add's users to my local administrator group. But what i would like to accoplish is to add a user from an active directory to my localmachine admin group. My code can add a local user to the local admin group. I tried to alter the LDAP string for the ActiveDirectory connection but now it fails to add the domain user to the localmachine admin group. The AD LDAP String: ldap://SERVERNAME01:389/CN=TESTUSER01,CN=Users,DC=DOMAIN01,DC=local The Test String: WinNT://WORKGROUP/STEPHAN-F894E19/TestUser1 The Test String works the AD LDAP String doesn't work. Can anyone help me with this? Thanks! the Code:

         private void AddUserToGroup() 
          { 
              try 
              { 
                  DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
                  DirectoryEntry grp; 
      
                  grp = AD.Children.Find("Administrators", "group"); 
                  if (grp != null) { grp.Invoke("Add", new object\[\] { "WinNT://WORKGROUP/STEPHAN-F894E19/TestUser1" }); } 
                  MessageBox.Show("Account Created Successfully"); 
              } 
              catch (Exception ex) 
              { 
                  MessageBox.Show(ex.Message); 
              } 
          }
      
      S Offline
      S Offline
      svh1985
      wrote on last edited by
      #2

      i Found this code on CodeProject:

      public void AddToGroup(string userDn, string groupDn)
      {
      try
      {
      DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
      dirEntry.Properties["member"].Add(userDn);
      dirEntry.CommitChanges();
      dirEntry.Close();
      }
      catch (System.DirectoryServices.DirectoryServicesCOMException E)
      {
      //doSomething with E.Message.ToString();

      }
      

      }

      But can anyone show me a sample LDAP String , UserDn String and a GroupDn String? Thanks!

      L 1 Reply Last reply
      0
      • S svh1985

        i Found this code on CodeProject:

        public void AddToGroup(string userDn, string groupDn)
        {
        try
        {
        DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
        dirEntry.Properties["member"].Add(userDn);
        dirEntry.CommitChanges();
        dirEntry.Close();
        }
        catch (System.DirectoryServices.DirectoryServicesCOMException E)
        {
        //doSomething with E.Message.ToString();

        }
        

        }

        But can anyone show me a sample LDAP String , UserDn String and a GroupDn String? Thanks!

        L Offline
        L Offline
        lane0p2
        wrote on last edited by
        #3

        LDAP string - LDAP://CN=Administrator,CN=Users,DC=aDomain,DC=com userDN - CN=aUser,CN=Users,DC=aDomain,DC=com DN means distinguishedName cool?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups