I've been assigned to modify a WinForms application to basically check that the logged on user belongs to a specific domain. This is what I've come up with so far:
byte[] domainSid;
var directoryContext =
new DirectoryContext(DirectoryContextType.Domain, "domain.se");
using (var domain = Domain.GetDomain(directoryContext))
using (var directoryEntry = domain.GetDirectoryEntry())
domainSid = (byte[])directoryEntry.Properties["objectSid"].Value;
var sid = new SecurityIdentifier(domainSid, 0);
bool validUser = UserPrincipal.Current.Sid.IsEqualDomainSid(sid);
Is there a better/easier way to do this? To me it seems like the domainSid would be accessible in some way using the PrincipalContext or some other class in System.Security.Principal.
I've considered using a hardcoded SID-string, but I don't know how "correct" that would be.
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.
Here's a slightly different approach to the accepted answer:
using System.Security.Principal;
using System.DirectoryServices;
//...
void Main()
{
var sidHelp = new SecurityIdentifierHelper();
Console.WriteLine(sidHelp.GetDomainSecurityIdentifier("MyDomain"));
}
//...
public class SecurityIdentifierHelper
{
const int OffsetBinaryFormStartingIndex = 0; // just to avoid magic numbers; though this is still a bit magic. Documentation says "The byte offset to use as the starting index in binaryForm", whatever that means.
// https://referencesource.microsoft.com/#mscorlib/system/security/principal/sid.cs,d06a8d5ee0c7a26d,references
// https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.securityidentifier.-ctor?view=netframework-4.8#System_Security_Principal_SecurityIdentifier__ctor_System_Byte___System_Int32_
public SecurityIdentifier GetDomainSecurityIdentifier(string domain)
{
var ldapPath = $"LDAP://{domain}";
var domainAdsi = new DirectoryEntry(ldapPath);
var domainSidByteArray = (byte[])domainAdsi.Properties["objectSid"].Value;
return new SecurityIdentifier(domainSidByteArray, OffsetBinaryFormStartingIndex);
}
}
Console.WriteLine(new System.Security.Principal.SecurityIdentifier(((byte[])new System.DirectoryServices.DirectoryEntry("LDAP://MyDomain").Properties["objectSid"].Value), 0));
[string]$myDomain = 'MyDomain' # or 'MyDomain.example.com'
[string]$ldapPath = 'LDAP://{0}' -f $MyDomain
[adsi]$domainAdsi = [adsi]$ldapPath
[System.Security.Principal.SecurityIdentifier]::new($domainAdsi.Value, 0)
[System.Security.Principal.SecurityIdentifier]::new(([ADSI]('LDAP://MyDomain')).Value, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With