Are there better (easier) ways to get a specific domain's SID?
What you're doing looks like the best option to me. Hardcoding strings is definetely not a good idea.
Each domain has a build in account domainName\administrator, so you can create an account with this name, translate it to the SecurityIdentifier and read the AccountDomainSid property.
An example of this way is:
public static class SecurityEx
{
public static SecurityIdentifier DomainSId
{
get
{
var administratorAcount = new NTAccount(GetDomainName(), "administrator");
var administratorSId = (SecurityIdentifier) administratorAcount.Translate(typeof (SecurityIdentifier));
return administratorSId.AccountDomainSid;
}
}
internal static string GetDomainName()
{
//could be other way to get the domain name through Environment.UserDomainName etc...
return IPGlobalProperties.GetIPGlobalProperties().DomainName;
}
}
Also you can find other solutions to achieve the same result via WMI or Lsa. This one seems the most elegant way for me.