How to get active directory users with logon workstation in c#

154 Views Asked by At

I am writing a code for get all active directory user details with System.DirectoryServices but i didn't find a way to get logon workstation (LogOnTo field) of each user.

Is there any way to get users workstation name? thank you for your time and sharing your knowledge.

1

There are 1 best solutions below

0
Shabnam Tabar On BEST ANSWER

in System.DirectoryServices you can user LDAP Filter (&(objectCategory=User)(objectClass=person)) to get AD Account Details and you can try the folowing for get user workstation's name:

 DirectoryEntry ldapConnection = ADProvider.createDirectoryEntry(_Domain.DomainName, _Domain.DomainController, _Domain.Username, _Domain.Password, _targetOU);
                DirectorySearcher search = new DirectorySearcher(ldapConnection, "(&(objectCategory=User)(objectClass=person))", null, SearchScope.OneLevel);
                SearchResultCollection results = search.FindAll();
                if (results != null)
                {
                   
                    foreach (SearchResult sr in results)
                    {
                       sr.Properties["userworkstations"][0].ToString();
                    }
                }