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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Get list of groups for a given user using ActiveDirectory

Get list of groups for a given user using ActiveDirectory

Scheduled Pinned Locked Moved C#
questioncomsysadmintutorialworkspace
2 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.
  • Q Offline
    Q Offline
    QuickDeveloper
    wrote on last edited by
    #1

    Hi I am getting the list of groups for a particular user using ActiveDirectory. I am using the following code.

    void GetUserDetailsFromAD()
    {
    string _userName = Environment.UserName;
    string _userDomain = Environment.UserDomainName;

    //Use given URL
    using (DirectoryEntry de = new DirectoryEntry(string.Format("LDAP://URL /OU=XYZ,DC={0},DC=XYZ,DC=XYZ,DC=com",_userDomain)))
    {
    //Create a text file to write details
    TextWriter tw = new StreamWriter("C:\\LDAP.txt");

                    de.AuthenticationType = AuthenticationTypes.Secure |AuthenticationTypes.ReadonlyServer;
                    using (DirectorySearcher search = new DirectorySearcher(de))
                    {
                        search.Filter = "(sAMAccountName=" + \_userName + ")";
                        search.PropertiesToLoad.Add("displayName");
                        search.PropertiesToLoad.Add("memberof");
                        SearchResult result = search.FindOne();
    
    
                        if (result != null)
                        {
                            \_displayName = result.Properties\["displayname"\]\[0\].ToString();
    
                            //Loop through all the groups that user is part of
                            foreach (object item in result.Properties\["memberof"\])
                            {
    
                                 //Write the groups to a text file
                                tw.WriteLine(item.ToString());
    
                              //Check whether the group in which user is present starts with ABC
                                if (item.ToString().StartsWith("CN=ABC,DC=DEF"))
    			{
    				//user is member of ABC group
    			}
    			else
    			{
    			       //user is not member
    			}
                            }
                        } 
                    }
    
                    tw.Close();
               
              
            }
    

    }

    When i run these program from my local machine(let's say machine A) ,the list of groups which _userName belongs to is populated in C:\\LDAP.txt file.However if i run this from another machine (machine B) in same network,only some groups are displayed ,not all.Eg:If machine A lists group 1,2,3,4 ,machine B lists only 1,2. 1)How can i know whether there are any restrictions in place on other system?(due to which all groups are not displayed) 2)How to check whether there any changes in network settings in the second machine with respect to Active Direc

    A 1 Reply Last reply
    0
    • Q QuickDeveloper

      Hi I am getting the list of groups for a particular user using ActiveDirectory. I am using the following code.

      void GetUserDetailsFromAD()
      {
      string _userName = Environment.UserName;
      string _userDomain = Environment.UserDomainName;

      //Use given URL
      using (DirectoryEntry de = new DirectoryEntry(string.Format("LDAP://URL /OU=XYZ,DC={0},DC=XYZ,DC=XYZ,DC=com",_userDomain)))
      {
      //Create a text file to write details
      TextWriter tw = new StreamWriter("C:\\LDAP.txt");

                      de.AuthenticationType = AuthenticationTypes.Secure |AuthenticationTypes.ReadonlyServer;
                      using (DirectorySearcher search = new DirectorySearcher(de))
                      {
                          search.Filter = "(sAMAccountName=" + \_userName + ")";
                          search.PropertiesToLoad.Add("displayName");
                          search.PropertiesToLoad.Add("memberof");
                          SearchResult result = search.FindOne();
      
      
                          if (result != null)
                          {
                              \_displayName = result.Properties\["displayname"\]\[0\].ToString();
      
                              //Loop through all the groups that user is part of
                              foreach (object item in result.Properties\["memberof"\])
                              {
      
                                   //Write the groups to a text file
                                  tw.WriteLine(item.ToString());
      
                                //Check whether the group in which user is present starts with ABC
                                  if (item.ToString().StartsWith("CN=ABC,DC=DEF"))
      			{
      				//user is member of ABC group
      			}
      			else
      			{
      			       //user is not member
      			}
                              }
                          } 
                      }
      
                      tw.Close();
                 
                
              }
      

      }

      When i run these program from my local machine(let's say machine A) ,the list of groups which _userName belongs to is populated in C:\\LDAP.txt file.However if i run this from another machine (machine B) in same network,only some groups are displayed ,not all.Eg:If machine A lists group 1,2,3,4 ,machine B lists only 1,2. 1)How can i know whether there are any restrictions in place on other system?(due to which all groups are not displayed) 2)How to check whether there any changes in network settings in the second machine with respect to Active Direc

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      This[^] might provide some assistance.

      Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
      Honestly. It's the honest ones you want to watch out for...

      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