internal static bool AreSidsInSameDomain(SecurityIdentifier sid1, SecurityIdentifier sid2) { if (!sid1.IsAccountSid() || !sid2.IsAccountSid()) { return false; } else { return sid1.AccountDomainSid.Equals(sid2.AccountDomainSid); } }
private string GetUpnFromSelection(UnsafeNativeMethods.DsSelection selection) { if (!string.IsNullOrEmpty(selection.pwzUPN)) { return selection.pwzUPN; } const string sidPropertyName = "objectSid"; string upn = selection.pwzADsPath; // Try to get the UPN value from AD path try { using (DirectoryEntry entry = new DirectoryEntry(upn)) { if (entry.Properties.Contains(sidPropertyName)) { SecurityIdentifier sid = new SecurityIdentifier((byte[])entry.Properties[sidPropertyName].Value, 0); if (sid.IsAccountSid()) { NTAccount acc = (NTAccount)sid.Translate(typeof(NTAccount)); upn = acc.Value; } } } } catch (Exception e) { Trace.TraceError(e.Message); } return upn; }
static void Main(string[] args) { // domainSid: WindowsIdentity.GetCurrent().User.AccountDomainSid); if (args.Length > 0) { if (args[0].StartsWith("-?") || args[0].StartsWith("-h") || args[0].StartsWith("-help") || args[0].StartsWith("/?") || args[0].StartsWith("/h") || args[0].StartsWith("/help")) { ShowHelp(); } else if (Enum.IsDefined(typeof(WellKnownSidType), args[0])) { try { WellKnownSidType sidType = (WellKnownSidType)Enum.Parse(typeof(WellKnownSidType), args[0], false); SecurityIdentifier sid = null; if (args[0].StartsWith("Account")) { sid = new SecurityIdentifier(sidType, WindowsIdentity.GetCurrent().User.AccountDomainSid); } else { sid = new SecurityIdentifier(sidType, null); } NTAccount NTUser = (NTAccount)sid.Translate(typeof(System.Security.Principal.NTAccount)); Console.WriteLine("[" + sidType.ToString() + "]"); Console.WriteLine("Name=" + NTUser.ToString()); Console.WriteLine("Shortname=" + NTUser.ToString().Substring(NTUser.ToString().IndexOf("\\")+1)); Console.WriteLine("SID=" + sid.ToString()); Console.WriteLine("IsAccountSid=" + sid.IsAccountSid().ToString().ToUpper()); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { if (args[0].StartsWith("S-")) { try { SecurityIdentifier sid = new SecurityIdentifier(args[0]); NTAccount NTUser = (NTAccount)sid.Translate(typeof(System.Security.Principal.NTAccount)); Console.WriteLine("[" + sid.ToString() + "]"); Console.WriteLine("Name=" + NTUser.ToString()); Console.WriteLine("Shortname=" + NTUser.ToString().Substring(NTUser.ToString().IndexOf("\\") + 1)); Console.WriteLine("SID=" + sid.ToString()); Console.WriteLine("IsAccountSid=" + sid.IsAccountSid().ToString().ToUpper()); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { try { NTAccount NTUser = new NTAccount(args[0]); SecurityIdentifier sid = (SecurityIdentifier)NTUser.Translate(typeof(SecurityIdentifier)); Console.WriteLine("[" + NTUser.ToString() + "]"); Console.WriteLine("Name=" + NTUser.ToString()); Console.WriteLine("Shortname=" + NTUser.ToString().Substring(NTUser.ToString().IndexOf("\\") + 1)); Console.WriteLine("SID=" + sid.ToString()); Console.WriteLine("IsAccountSid=" + sid.IsAccountSid().ToString().ToUpper()); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } else { ShowHelp(); } }