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
Wednesday, May 13, 2009 6:48:29 PM (Central Standard Time, UTC-06:00)
Would it be better to use StringComparison.OrdinalIgnoreCase in this case?

if (Environment.UserDomainName.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase))
Greg Law
Thursday, May 14, 2009 8:39:33 AM (Central Standard Time, UTC-06:00)
So noted!
Thursday, May 14, 2009 1:10:30 PM (Central Standard Time, UTC-06:00)
Why not use the System.DirectoryServices.AccountManagement namespace?

something like...

using (
var principalContext = new PrincipalContext(ContextType.Domain, domainName, adUsername, adPassword))
{
using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, username))
{
string emailAddress = userPrincipal.EmailAddress ?? "";
var nameParts = new List<string>
{userPrincipal.GivenName, userPrincipal.MiddleName, userPrincipal.Surname};
string[] notEmptyNameParts =
(from n in nameParts where n != null && n.Trim() != string.Empty select n).ToArray();
string fullName = string.Join(" ", notEmptyNameParts);
}
}
Thursday, May 28, 2009 10:14:18 AM (Central Standard Time, UTC-06:00)
Morten - unless I'm mistaken, your snippet won't work for the workgroup case (non-domain member), whereas the snippet in my post covers that.
Comments are closed.
Archive
<February 2010>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213
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)