31663

Get distinguished name from Active Directory of currently logged in user

Question:

How can I get the distinguished name from Active Directory of the currently logged in user in C#?

Answer1:

Check following snippet. You have pass to Identity.Name from <a href="http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.aspx" rel="nofollow">IPrincipal</a>. I assume that the user is already authenticated in Active Directory (ie. using standard IIS authorization methods).

private string GetUserName(string identity) { if (identity.Contains("\\")) { string[] identityList = identity.Split('\\'); return identityList[1]; } else { return identity; } } public string GetUserDn(string identity) { var userName = GetUserName(identity); using (var rootEntry = new DirectoryEntry("LDAP://" + adConfiguration.ServerAddress, null, null, AuthenticationTypes.Secure)) { using (var directorySearcher = new DirectorySearcher(rootEntry, String.Format("(sAMAccountName={0})", userName))) { var searchResult = directorySearcher.FindOne(); if (searchResult != null) { using (var userEntry = searchResult.GetDirectoryEntry()) { return (string)userEntry.Properties["distinguishedName"].Value; } } } } return null; }

Answer2:

Why wouldn't you just use: System.DirectoryServices.AccountManagement.UserPrincipal.Current.DistinguishedName

Recommend

  • How to pass a variable value from one JFrame to Another JFrame in Netbeans
  • Why can't I catch EJB exceptions?
  • Object has null values when subclass attempts to use it. Why?
  • Get the computer user name in a web application
  • Using Paypal Pro in Omnipay
  • CS1703: In Xamarin.Droid, should I use the .Net Standard windowsruntime.dll located in Mono.Framewor
  • Uber API - requests endpoint cannot read read json
  • Add Windows Feature from C#
  • Owin Authentication and claims in asp.net how to access user data
  • msbuild create itemgroup from property group
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Swing - Get new component under mouseReleased
  • MVC3 Razor - ListBox pre-select not working
  • Set the selected item in dropdownlist in MVC3
  • Change Inet root folder for iis 7
  • ilmerge with a PFX file
  • Spring Data JPA custom method causing PropertyReferenceException
  • Can I display google adwords (AdView) in javafx on android
  • Getting last autonumber in access
  • swift auto completion not working in Xcode6-Beta
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Revoking OAuth Access Token Results in 404 Not Found
  • C# - Getting references of reference
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • File not found error Google Drive API
  • apache spark aggregate function using min value
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?