locked
Trying to Retrieve AD User Info Using C# 2005 RRS feed

  • Question

  • User579668530 posted

    I am trying to connect to AD to retrieve employee information from 3 trees in the same forest (i.e. americas.domain.com, asia.domain.com, etc).  The server I am working with is in the americas.domain.com tree and I can connect and get information just fine.  What I need to do is be able to connect at the root (domain.com) of the forest so I can get everyones information.  I have a user account that has been created at the root of the forest.  I am using the below code to try to connect:

    string domainAndUsername = @"LDAP:// <Domain> ";
    string userName = "user";
    string passWord = "password";

    AuthenticationTypes at = AuthenticationTypes.Anonymous;
    at = AuthenticationTypes.Secure;

    DirectoryEntry entryRoot = new DirectoryEntry(domainAndUsername, userName, passWord, at);

    DirectorySearcher mySearcher = new DirectorySearcher(entryRoot);
    SearchResultCollection results;
    mySearcher.Filter = String.Format("(cn={0})", userName);
    results = mySearcher.FindAll();

     When it gets to the "results" I get username or password invalid.  I am new to c# and am not sure where to go from here.

     also, I have tested the account to make sure it works by writing a classic ASP page and it connects just fine with the following code.

    ' ------ SCRIPT CONFIGURATION ------
    strBase    = "<GC://<domain>>;"
    strFilter  = "(&(objectCategory=person)(objectClass=user));"
    strAttrs   = "sn, cn, distinguishedName;"
    strScope   = "Subtree"
    ' ------ END CONFIGURATION ---------

    sUser   = "user"
    sPassword  = "password"

    set objConn = CreateObject("ADODB.Connection")
    objConn.Provider = "ADsDSOObject"
    objConn.Open "Active Directory Provider", sUser, sPassword
    Response.Write strBase & strFilter & strAttrs & strScope & "<br />"
    set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
    objRS.MoveFirst
    while Not objRS.EOF
        Response.Write objRS.Fields(0).Value & " : " & objRS.Fields(1).Value & " : " & objRS.Fields(2).Value &  "<br />"
        objRS.MoveNext
    wend

    Monday, May 21, 2007 6:50 PM

Answers

All replies

  • User-319574463 posted

    I suggest that you download BeaverTail and its source from http://adsi.mvps.org/adsi/csharp/beavertail.html - it does a lot of what you want.

    Remember than an LDAP query will return a maimum of 1000 rows.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2007 3:12 AM
  • User579668530 posted

    Thanks for the advise on the application.  It helped me to find my issue.  I actually got the same error with the username and password using the beavertail app.  I decided to try useing "username@domain.com" for the username just in case I was loosing scope being at the root.  It fired right in and gave me all the info I needed.  I did the same thing for my .net and I was able to access LDAP.  The app also helped with some of the retrieval issues I was having.

     thanks again.

    Tuesday, May 22, 2007 1:42 PM
  • User-319574463 posted

    As the problem is now solved, please mark the thread as answered.

    Tuesday, May 22, 2007 1:50 PM