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];}
Scott Colestock lives, writes, and works as an independent consultant in the Twin Cities (Minneapolis, Minnesota) area.