C#-web: Reading Users Of Active Directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhanashivam
    New Member
    • Mar 2007
    • 31

    C#-web: Reading Users Of Active Directory

    Hi All,

    I am using the DirectorySearch er class for reading the active directory users. But the result shows the IUSR accounts also.

    I dont want to take the IUSR accounts.

    So please help me to take the usernames with out IUSR accounts from the active directory.

    Is any attributes for remove those IUSR accounts?

    Thanks in Advance.

    Regards,
    Dhanasekaran. G
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Do a post process on your data, removing the IUSER accounts?
    Offhand I don't know of any attributes, but you could look at them in windows and see if there is anything in common

    Comment

    • mikeyeli
      New Member
      • Oct 2007
      • 63

      #3
      Originally posted by dhanashivam
      Hi All,

      I am using the DirectorySearch er class for reading the active directory users. But the result shows the IUSR accounts also.

      I dont want to take the IUSR accounts.

      So please help me to take the usernames with out IUSR accounts from the active directory.

      Is any attributes for remove those IUSR accounts?

      Thanks in Advance.

      Regards,
      Dhanasekaran. G
      you must specify what you want at the DIrectorySearch er filter
      ex C#:
      Code:
      search.Filter = "(&(objectClass=user)(objectCategory=person))";
      Here is an example:
      Code:
      public static ArrayList GetAllADDomainUsers(string domainpath)
      {
              ArrayList allUsers = new ArrayList();
              DirectoryEntry searchRoot;
              
              searchRoot = new DirectoryEntry(domainpath);
      
              DirectorySearcher search = new DirectorySearcher(searchRoot);
              search.Filter = "(&(objectClass=user)(objectCategory=person))";
              search.PropertiesToLoad.Add("samaccountname");
      
              SearchResult result;
              SearchResultCollection resultCol = search.FindAll();			
      
              if (resultCol != null)
              {
                      for(int counter=0; counter < resultCol.Count; counter++)
                      {
                              result = resultCol[counter];
                              if (result.Properties.Contains("samaccountname"))
                              {
                                      allUsers.Add((String)result.Properties["samaccountname"][0]);
                              }
                      }
              }
              return allUsers;
      }

      Comment

      • dhanashivam
        New Member
        • Mar 2007
        • 31

        #4
        Hi,

        thanks for your reply. But still i am getting the following user accounts.

        IWAM_DOMAINCS, IUSR_DOMAINCS,

        my code is:

        DirectorySearch er Searcher = new DirectorySearch er();
        Searcher.Search Scope = SearchScope.Sub tree;
        Searcher.Proper tiesToLoad.Add( "sAMAccountName ");
        Searcher.Filter = "(&(objectClass =user)(objectCa tegory=person)) ";

        DataTable dtUsers = new DataTable("User InAD");
        dtUsers.Columns .Add("uname");
        string str = "";
        foreach (SearchResult AdObj in Searcher.FindAl l())
        {
        str += Convert.ToStrin g(AdObj.Propert ies["sAMAccountName "][0]) + "\n";
        DataRow dr = dtUsers.NewRow( );
        dr[0] = Convert.ToStrin g(AdObj.Propert ies["sAMAccountName "][0]);
        dtUsers.Rows.Ad d(dr);
        }

        please help me.

        thanks in advance.

        Regards,
        Dhanasekaran. G

        Comment

        • mikeyeli
          New Member
          • Oct 2007
          • 63

          #5
          Originally posted by dhanashivam
          Hi,

          thanks for your reply. But still i am getting the following user accounts.

          IWAM_DOMAINCS, IUSR_DOMAINCS,

          my code is:

          DirectorySearch er Searcher = new DirectorySearch er();
          Searcher.Search Scope = SearchScope.Sub tree;
          Searcher.Proper tiesToLoad.Add( "sAMAccountName ");
          Searcher.Filter = "(&(objectClass =user)(objectCa tegory=person)) ";

          DataTable dtUsers = new DataTable("User InAD");
          dtUsers.Columns .Add("uname");
          string str = "";
          foreach (SearchResult AdObj in Searcher.FindAl l())
          {
          str += Convert.ToStrin g(AdObj.Propert ies["sAMAccountName "][0]) + "\n";
          DataRow dr = dtUsers.NewRow( );
          dr[0] = Convert.ToStrin g(AdObj.Propert ies["sAMAccountName "][0]);
          dtUsers.Rows.Ad d(dr);
          }

          please help me.

          thanks in advance.

          Regards,
          Dhanasekaran. G
          You're right, ive been searching, cant find a way to directly removing them on the filter, i think you should follow platers advice, do some post proccess to remove these accounts, although you have to think a way to make this efficient, since there can be a considerable amount of users. sorry i couldnt help

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I think there is a way to use SQL to query against active directory. (I saw it somewhere burried in msdn once i think)
            That might be useful to throw in a "WHERE UserName NO like 'IUSER%' " or something.
            Maybe you can do that with DirectorySearch er too though?

            Comment

            Working...