Can't query AD (get a DirectoryServicesCOMException)

4

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?

c#
asp.net
windows
windows-server-2008
directoryservices
asked on Stack Overflow Aug 12, 2010 by KevinDeus

4 Answers

3

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

answered on Stack Overflow Aug 12, 2010 by John
0

You can also change the IIS Application Pool to run a domain account with the query priveleges you are searching for.

I have a few other comments as well:

  1. Make sure the first entry for the DirectoryEntry constructor includes the container for the users as well. This should help the DirectorySearcher to work more reliably.
  2. I believe the second parameter in the DirectoryEntry constructor should be the user name, not the AD query path.
  3. You should set the AuthenticationType property as well. With Server 2008, by default, this needs to be set to AuthenticationTypes.Secure | AuthenticationTypes.ServerBind | AuthenticationTypes.Sealing. I'd guess that 2008R2 has a simliar requirement.
answered on Stack Overflow Aug 12, 2010 by Jeff Siver
0

I see that the question is rather old, but after struggling with this I thought to mention that it is indeed possible to use the LDAP-style of the username (in opposite to the DNS style). This works well for me:

    string connString = "LDAP://MyDomain/CN=blah,DC=blah,DC=blah";
    string username = "CN=MyAdmin,CN=Users,CN=blah,DC=blah,DC=blah";
    string password = "myLittleSecret";
    DirectoryEntry root = new DirectoryEntry(
         connString, 
         username, 
         password, 
         AuthenticationTypes.None);

Where MyAdmin is a member in the Administrators role.

One little thing that took me a while to find is the AuthenticationTypes.None parameter that is needed if you do not want to communicate over SSL. Surely, you want to do this in production, but for development purposes it may be OK to skip the encryption.

Environment: Windows 7

answered on Stack Overflow Sep 9, 2012 by Avada Kedavra • edited Sep 11, 2012 by Avada Kedavra
0

I was also getting this exception when tried to query the active directory:

SearchResult result = srch.FindOne();

To resolve this, just put the above code inside Security.RunWithElevatedPrivileges().

Final Solution:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    result = srch.FindOne();
});
answered on Stack Overflow May 30, 2013 by nbi • edited May 30, 2013 by Simon MᶜKenzie

User contributions licensed under CC BY-SA 3.0