in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Michael Hofer - SharePoint Blog

Michael Hofer's blog about adventures in SharePoint land, including tips and tricks for all products and technologies used in Information Worker solutions.

ActiveDirectory/LDAP: How to get the logon name of a user?

This post demonstrates how to construct the user logonname from an ActiveDirectory DirectoryEntry instance using C#.

 Well, this time nothing about SharePoint! Wow! I've decided to post this little "How To" since I've encountered this problem already many times and spent always a heck of a time looking for an answer.

Situation

I have the DirectoryEntry instance representing a user-objectClass in ActiveDirectory and I want to get his/her Windows logonname using not the full qualified name, but "<domain>\<username>" format.

Solution

Use the "distinguishedName" property and extract the first "DN=xyz" information. This is tipically the domain name you are looking for. The username portion comes then easily from the "samaccountname" property.

Example:

string distinguishedName = ((string)entry.Properties["distinguishedName"].Value);
Regex regex = new Regex("(?<=DC=).+?(?=,)");
Match match = regex.Match(distinguishedName);
if (match.Success)
{
    return string.Format("WinNT://{0}/{1}", match.Value.ToUpper(), entry.Properties["samaccountname"][0]);
}
else
    ...
 

Comments

 

paisleygo said:

isn't there an easier way to do this in SHarepoint?

December 12, 2007 6:41 PM
 

MK said:

Hi,

Can you please let me know if this works in the app which is localized.

thanks

MK

April 11, 2008 11:18 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

About mhofer1976

For all of my IT carreer, I've been addicted to Microsoft-based software development, starting with VB, but then heading straight towards the .NET Framework in its earliest days. While working in different positions as a lead developer, project manager, pre-sales consultant and most recently a business unit manager, I've always kept beeing a developer and specialized on solutions in the Information Worker area, with an emphasis towards Enterprise Content Management. The Microsoft Office System and especially the SharePoint products and technologies are my favorite "playground", one big reason why I've joined the Microsoft Consulting Services in Switzerland where I'm currently working as Senior Consultant for Information Worker solutions.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts