Get Active Directory entry from SID

In order to get the DirectoryEntry we need create an NTAccount object with the domain user account we can get and traslate it to a SecurityIdentifier:

    NTAccount account =

        new NTAccount(@"<Domain Name>\<User Account Name>");

    SecurityIdentifier sid =

        (SecurityIdentifier)account.Translate(

        typeof(SecurityIdentifier));

Once the SecurityIdentifier is get it we can get the DirectoryEntry as follow:

    const string SidSearchFormat = "LDAP://<SID={0}>";

    DirectoryEntry userEntry = new DirectoryEntry(

        string.Format(SidSearchFormat, sid.Value));

That is the better way to get an user account from the Active Directory because the binding to the data does not involve any search. It can be used for any kind of object in your AD.

6 Comments

Comments have been disabled for this content.