Throw DirectoryServicesCOMException (0x80072020) when try to RefreshCache for DirectoryEntry

0

I write a very sample test program and run it as local system account in a domain machine. Here is the code look like:

static void Main(string[] args)
{
    try
    {
        System.Console.Out.WriteLine("Test Start");
        List<string> temp = new List<string>();
        temp.Add(Environment.UserDomainName);
        temp.Add("test");
        temp.Add("test.com");
        temp.Add("dc.test.com");
        temp.Add("gc.test.com");

        foreach (var i in temp)
        {

            using (HostingEnvironment.Impersonate())
            {
                System.Console.WriteLine("LDAP://{0}", i);
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + i);
                try
                {
                    entry.RefreshCache();
                    string nativeGuid = entry.NativeGuid;
                    string path = entry.Path;
                    string server = entry.Options.GetCurrentServerName();
                    System.Console.WriteLine("{0} success!", i);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("{0}\n {1}", i, e);
                }
            }
        }

        System.Console.Out.WriteLine("Test End");
    }
    catch (Exception e)
    {
        System.Console.Out.WriteLine("e:Main{0}", e.Message);
    }
    System.Console.In.ReadLine();
}

The NetBIOS name for the domain is "test", full domain name is "test.com". "dc.test.com" is the DC FQDN and "gc.test.com" is the GC FQDN.

It works fine for "test.com", "dc.test.com"" and "gc.test.com", but it throws DirectoryServicesCOMException (0x80072020) for "test" and "Environment.UserDomainName".

The detail running result is:

Test Start
LDAP://TEST
TEST
 System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operati
ons error occurred.

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.RefreshCache()
   at ConsoleApplication1.Program.Main(String[] args)
LDAP://test
test
 System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operati
ons error occurred.

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.RefreshCache()
   at ConsoleApplication1.Program.Main(String[] args)
LDAP://test.com
test.com success!
LDAP://dc.test.com
dc.test.com success!
LDAP://gc.test.com
gc.test.com success!
Test End

It works all fine if I run it as domian admin account. Any idea what cause this? Thanks a lots!

active-directory
ldap
directoryentry
asked on Stack Overflow Aug 5, 2013 by Vincent

1 Answer

0

What are you actually trying to do? If you're on a machine joined to the domain, you should just do new DirectoryEntry().

As for your error, when you log on to Windows with a local account, the UserDomainName environment variable is set to the local computer name. If that machine's name is the same as the domain's NetBIOS name, then I wouldn't be surprised if Windows gets confused.

answered on Stack Overflow Aug 5, 2013 by Sean Hall

User contributions licensed under CC BY-SA 3.0