a trace of thought on...BizTalk Server, Team Foundation Server, Windows Mobile, etc. RSS 2.0
 Wednesday, May 13, 2009
You see quite a few code samples for retrieving "FullName" (or other properties) from an Active Directory that look like this:
string directoryPath = "WinNT://somedomain/someuser";
string fullName;

DirectoryEntry directoryEntry = new DirectoryEntry(directoryPath);
if (directoryEntry.Properties["FullName"].Value != null)
{
fullName = directoryEntry.Properties["FullName"].Value.ToString();
}

One issue with this code is that when "somedomain" actually corresponds to the local machine name (because your are trying to retrieve the "FullName" of a local account), it can take an extremely long time to run. This is probably because the underlying framework is off trying to find a domain controller before falling back to the local security store.

To work around this, consider code that would look like the following when formulating the directory path:

string[] identities = windowsIdentity.Name.Split('\\');
string directoryPath;
if(Environment.UserDomainName.Equals(Environment.MachineName,StringComparison.OrdinalIgnoreCase))
{
// special case needed for "off domain" case.
directoryPath = "WinNT://localhost/" + identities[1];
}
else
{
directoryPath = "WinNT://" + identities[0] + "/" + identities[1];
}
Using "localhost" will cause the property retrieval to be just about instantaneous, whereas using the computer name (from a local account Windows identity) results in up to a twelve second delay.

Wednesday, May 13, 2009 8:24:59 AM (Central Standard Time, UTC-06:00)  #    Comments [4] -
General
Archive
<May 2009>
SunMonTueWedThuFriSat
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author:

Scott Colestock lives, writes, and works as an independent consultant in the Twin Cities (Minneapolis, Minnesota) area.

© Copyright 2010
Scott Colestock
Sign In
All Content © 2010, Scott Colestock
DasBlog theme 'Business' created by Christoph De Baene (delarou)