Search code examples
c#active-directorydirectoryservices

How to get domain sid from domain dns name?


I have the dns name of an Active Directory domain and I would like to get the domain sid for this domain. How can I do this?

I already have the following:

var domainContext =
    new DirectoryContext(DirectoryContextType.Domain, domainDnsName);
var domain = Domain.GetDomain(domainContext);
var dc = domain.DomainControllers.OfType<DomainController>().FirstOrDefault();

But I'm a little stuck where to go from here (or maybe I'm following the wrong route).


Solution

  • what you want is something like this:

    DirectoryEntry domainEntry = domain.GetDirectoryEntry();
    byte[] domainSid = domainEntry.Properties["objectSID"].Value as byte[];
    SecurityIdentifier strongDomainSid = new SecurityIdentifier(domainSid, 0);