Can't query AD (get a DirectoryServicesCOMException)

21,108

Change the username parameter from "cn=xxx, dc=yyy, dc=zzz" to "Domain\Username"

Share:
21,108
KevinDeus
Author by

KevinDeus

C# JQuery nunit WCF Subversion SharpSVN TFS RoombaSCI CreateOI Framework ChessMangler

Updated on July 09, 2022

Comments

  • KevinDeus
    KevinDeus almost 2 years

    I'm attempting to query AD in an ASP.Net (4.0) application that is running on Windows Server 2008 R2 (IIS7 installed). (It also fails when running as a 2.0 application as well)

    This is nothing new for me, as I've done this many times before. I wrote a small ASP.Net program that runs fine on my own machine (Windows XP with IIS6), but fails when run on the 2008 box.

    (The result is that you see a list of groups the user is a member of in a textbox)

    (on button_click) 
    var userName = txtUserName.Text;
    
    if (userName.Trim().Length == 0)
    {
         txtResults.Text = "-- MISSING USER NAME --";
         return;
    }
    
    var entry = new DirectoryEntry("LDAP://blah.blah/DC=blah,DC=blah",
                                   "cn=acct, dc=blah, dc=blah",
                                   "pass");
    
    var search = new DirectorySearcher(entry);
    search.Filter = "(SAMAccountName=" + userName + ")";
    search.PropertiesToLoad.Add("memberOf");
    
    var groupsList = new StringBuilder();
    
    var result = search.FindOne();
    
    if (result != null)
    {
       int groupCount = result.Properties["memberOf"].Count;
    
       for (int counter = 0; counter < groupCount; counter++)
       {
               groupsList.Append((string)result.Properties["memberOf"][counter]);
               groupsList.Append("\r\n");
        }
    }
    
    txtResults.Text = groupsList.ToString();
    

    When I run this code I get the following error on search.FindOne():

    System.DirectoryServices.DirectoryServicesCOMException (0x8007203B): A local error has occurred.
    
       at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
       at System.DirectoryServices.DirectoryEntry.Bind()
       at System.DirectoryServices.DirectoryEntry.get_AdsObject()
       at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
       at System.DirectoryServices.DirectorySearcher.FindOne()
       at WebApplication1._Default.btnSearch_Click(Object sender, EventArgs e)
    

    We've done a lot of research with this and twiddled every IIS7 setting we can think of, but no go so far. Any clues?

  • Ghazaly
    Ghazaly about 13 years
    Had the same issue on Windows 7, but it works fine on WinXP. Thankfully my colleague sent me this post. Thanks.