D
D
Dmitry2015-02-21 20:04:58
Active Directory
Dmitry, 2015-02-21 20:04:58

.NET 4.x Get username from AD, type: Ivanov Ivan Ivanovich?

Good afternoon.
Please tell me how you can get a username from Active Directory, a user on a PC under a domain account.
Get data of this type: Ivanov Ivan Ivanovich

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2015-02-21
@Sumor

There are two ways.
The first is through the System.DirectoryServices.dll library (you need to additionally include it in References (links)).
Use class DirectoryEntry, DirectorySearcher.
The second is to use the ODBC driver for ActiveDirectory.
When using both paths, one must take into account the somewhat unusual structure and methods of working with ActiveDirectory.
For example, to return all users, you need a query like LDAP://CN=users,DC=server,DC=com
An ODBC query to return usernames should look something like this (there is no way to check for sure):

SELECT FullName FROM 'LDAP://CN=users,DC=server,DC=com'

D
Dmitry, 2015-03-12
@sharovd

I found a solution that displays the full name from AD by login.
Need to connect System.DirectoryServices

private void getUser(string uLogin)
        {
            string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", uLogin);
            string domain = "[ВАШ_ДОМЕН]";
            string[] properties = new string[] { "fullname" };
 
            DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
            DirectorySearcher searcher = new DirectorySearcher(adRoot);
            searcher.SearchScope = SearchScope.Subtree;
            searcher.ReferralChasing = ReferralChasingOption.All;
            searcher.PropertiesToLoad.AddRange(properties);
            searcher.Filter = filter;
 
            SearchResult result = searcher.FindOne();
            DirectoryEntry directoryEntry = result.GetDirectoryEntry();
          
            string displayName = directoryEntry.Properties["displayName"][0].ToString();
            MessageBox.Show(displayName);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question