Given a user's SID, how can I get the AD DirectoryEntry?

mtm picture mtm · Aug 17, 2011 · Viewed 14k times · Source

I have the user's SID as byte[] within windowsPrincipal.getIdentity().getSid(). How can I get an Active Directory entry (DirectoryEntry) from the SID?

Answer

Mathew Leger picture Mathew Leger · Feb 24, 2014

Use the SecurityIdentifier class to convert the sid from byte[] format to string and then bind directly to the object:

DirectoryEntry OpenEntry(byte[] sidAsBytes)
{
    var sid = new SecurityIdentifier(sidAsBytes, 0);

    return new DirectoryEntry(string.Format("LDAP://<SID={0}>", sid.ToString()));
}