{"id":237,"date":"2006-03-01T15:01:54","date_gmt":"2006-03-01T14:01:54","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=237"},"modified":"2006-03-10T15:33:18","modified_gmt":"2006-03-10T14:33:18","slug":"using-ldap-to-locate-a-user-name","status":"publish","type":"post","link":"https:\/\/www.craigmurphy.com\/blog\/?p=237","title":{"rendered":"Using LDAP to locate a user name"},"content":{"rendered":"<p>I found myself needing to extract the current user&#8217;s full name from our Active Directory today.  For a variety of reasons, I&#8217;ve not done too much work in this area, so I had to hunt around for a few minutes before arriving at a solution.<\/p>\n<p>Firstly, I had to add a reference to System.DirectoryServices to my project &#8211; no problem, right click on the References node in Visual Studio and choose Add Reference.  Secondly, I had to add using System.DirectoryServices to my source code.<\/p>\n<p>Anyway, here&#8217;s the C# code that I used &#8211; note the use of the UserName property from the  <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemenvironmentclasstopic.asp\">Environment class<\/a>.<\/p>\n<p><code>using System.DirectoryServices;<br \/>\n...<br \/>\nDirectoryEntry directoryEntry = new DirectoryEntry(\"LDAP:\/\/dc=DOMAIN_CONTEXT,dc=com\");<br \/>\nDirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);<br \/>\ndirectorySearcher.Filter  = \"(&(objectCategory=person)(sAMAccountName=\" + Environment.UserName + \"))\";<br \/>\nDirectoryEntry result = directorySearcher.FindOne().GetDirectoryEntry();<\/p>\n<p>string name  = result.Properties[\"<strong>displayName<\/strong>\"].Value.ToString();<\/code><\/p>\n<p>In my case, the username CMurphy became Craig Murphy (displayName).  Your properties may vary, and you&#8217;ll need to know your own domain context (dc).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I found myself needing to extract the current user&#8217;s full name from our Active Directory today. For a variety of reasons, I&#8217;ve not done too much work in this area, so I had to hunt around for a few minutes before arriving at a solution. Firstly, I had to add a reference to System.DirectoryServices to &hellip; <a href=\"https:\/\/www.craigmurphy.com\/blog\/?p=237\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using LDAP to locate a user name<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-237","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=237"}],"version-history":[{"count":0,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/237\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}