0

I have followed this link C# LDAP query to retrieve all users in an organisational unit and "A referral was returned from the server" exception when accessing AD from C#

I need to know what I am doing wrong in my LDAP path ?

 // create your domain context and define what container to search in - here OU=Employees
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MS", "OU=Employees,DC=CompanyName,DC=com");

        // define a "query-by-example" principal - here, we search for a UserPrincipal 
        // that is still active
        UserPrincipal qbeUser = new UserPrincipal(ctx);
        qbeUser.Enabled = true;

        // create your principal searcher passing in the QBE principal    
        PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

        // find all matches
        foreach (var found in srch.FindAll())
        {
            // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
        }

I Need all users detail (Name, Email, Designation, Department) in the current organisation using C# and display those in a dropdownlist. Please help.

Community
  • 1
  • 1
Akshay
  • 1,412
  • 2
  • 17
  • 51
  • I need to know what I am doing wrong in my LDAP path ? – Akshay Oct 25 '16 at 09:25
  • According to your code, you're sending `MS` as the second parameter to the `PrincipalContext` constructor, which [is documented as](https://msdn.microsoft.com/en-us/library/bb348316(v=vs.110).aspx) _"The name of the domain or server for Domain context types"_ - is that the correct domain server name? – stuartd Oct 25 '16 at 10:05
  • I think yes because I need users with MS domain. I mean when we login to our systems we use ms/username and password. I need all MS domain users list with those details. If MS is incorrect and If I need to know the correct one, how can I find out? – Akshay Oct 26 '16 at 07:14
  • So your login name is (say) "ms\sak"? – stuartd Oct 26 '16 at 08:54
  • correct........ – Akshay Oct 26 '16 at 08:59

1 Answers1

1
public DataTable FindPersons(string lname, string fname)
    { 

        DirectorySearcher searcher = new DirectorySearcher();
        searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0}*)(sn={1}*))", fname, lname);

        SearchResultCollection allResults;
        allResults = searcher.FindAll();

        DataTable dt = new DataTable();
        dt.Columns.Add("DisplayName", typeof(string));
        dt.Columns.Add("GivenName", typeof(string));
        dt.Columns.Add("SurName", typeof(string));
        dt.Columns.Add("MSID", typeof(string));
        if (allResults.Count >= 0)
        { 
            for (int i = 0; i < allResults.Count; i++)
            {
                DirectoryEntry deMembershipUser = allResults[i].GetDirectoryEntry();
                deMembershipUser.RefreshCache(); 

                dt.Rows.Add(
                    (string)deMembershipUser.Properties["displayname"].Value, 
                    (string)deMembershipUser.Properties["givenName"].Value,
                    (string)deMembershipUser.Properties["sn"].Value, 
                    (string)deMembershipUser.Properties["cn"].Value
                    ); 
            } 
        } 
        return dt;
    }
Akshay
  • 1,412
  • 2
  • 17
  • 51
  • I know this is 3 years old but if you use the new way the code is DirectoryEntry directoryEntry = (DirectoryEntry)result.GetUnderlyingObject(); var department = directoryEntry.Properties["department"].Value; – Kevin Kohler Oct 29 '20 at 16:08