/// <summary> /// /// </summary> /// <param name="SID">SID to search for</param> /// <returns>AccountName</returns> public static string GetLocalUserNameFromSID(string sidString) { SecurityIdentifier sid = new SecurityIdentifier(sidString); NTAccount account = (NTAccount)sid.Translate(typeof(NTAccount)); return account.Value; }
private static string GetAccountNameFromSidInternal(SecurityIdentifier ident, RemoteServerSettings serverSettings) { string accountName = string.Empty; Log.WriteStart("GetAccountNameFromSidInternal"); if (ident != null) accountName = ident.Translate(typeof(NTAccount)).ToString(); if (!string.IsNullOrEmpty(accountName)) accountName = GetUserName(accountName, serverSettings); else Log.WriteError("Accountname could not determined", new SecurityException()); Log.WriteEnd("GetAccountNameFromSidInternal"); return accountName; }
static WindowsUserInfo() { // Determine built-in group list - this is not expected to change so it is statically cached List<string> builtInGroups = new List<string>(); WellKnownSidType[] builtInSids = { WellKnownSidType.BuiltinAccountOperatorsSid, WellKnownSidType.BuiltinAdministratorsSid, WellKnownSidType.BuiltinAuthorizationAccessSid, WellKnownSidType.BuiltinBackupOperatorsSid, WellKnownSidType.BuiltinDomainSid, WellKnownSidType.BuiltinGuestsSid, WellKnownSidType.BuiltinIncomingForestTrustBuildersSid, WellKnownSidType.BuiltinNetworkConfigurationOperatorsSid, WellKnownSidType.BuiltinPerformanceLoggingUsersSid, WellKnownSidType.BuiltinPerformanceMonitoringUsersSid, WellKnownSidType.BuiltinPowerUsersSid, WellKnownSidType.BuiltinPreWindows2000CompatibleAccessSid, WellKnownSidType.BuiltinPrintOperatorsSid, WellKnownSidType.BuiltinRemoteDesktopUsersSid, WellKnownSidType.BuiltinReplicatorSid, WellKnownSidType.BuiltinSystemOperatorsSid, WellKnownSidType.BuiltinUsersSid }; SecurityIdentifier securityIdentifier; NTAccount groupAccount; foreach (WellKnownSidType builtInSid in builtInSids) { try { // Attempt to translate well-known SID to a local NT group - if this fails, local group is not defined securityIdentifier = new SecurityIdentifier(builtInSid, null); groupAccount = (NTAccount)securityIdentifier.Translate(typeof(NTAccount)); // Don't include "BUILTIN\" prefix for group names so they are easily comparable builtInGroups.Add(groupAccount.ToString().Substring(8)); } catch (IdentityNotMappedException ex) { Logger.SwallowException(ex, "WindowsUserInfo.cs Static Constructor: Failed to lookup identity", builtInSid.ToString()); } } // Sort list so binary search can be used builtInGroups.Sort(StringComparer.OrdinalIgnoreCase); s_builtInLocalGroups = builtInGroups.ToArray(); }
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; }
/// <summary> /// Gets the member info. /// </summary> /// <param name="sid">The sid.</param> /// <param name="memberName">Name of the member.</param> /// <param name="isLocal">if set to <c>true</c> [is local].</param> public static void GetMemberInfo(string sid, out string memberName, out bool isLocal) { try { SecurityIdentifier SID = new SecurityIdentifier(sid); //SearchResult sr = null; NTAccount nta = ((NTAccount)SID.Translate(typeof(NTAccount))); isLocal = (nta.Value.StartsWith(Environment.MachineName)); memberName = nta.Value; //if (SID.AccountDomainSid.Equals(SID)) //!=null //{ // DirectoryEntry root = DirectoryServicesUtils.newDirectoryEntry("LDAP://" + SqlAzManStorage.RootDSEPath); // DirectorySearcher ds = new DirectorySearcher(root, String.Format("(&(objectSid={0}))", sid)); // sr = ds.FindOne(); //} //if (sr != null) //{ // DirectoryEntry de = sr.GetDirectoryEntry(); // isLocal = false; // memberName = (string)de.Properties["samaccountname"].Value; // if (String.IsNullOrEmpty(memberName)) // { // isLocal = false; // //NTAccount nta = (NTAccount)SID.Translate(typeof(NTAccount)); // memberName = nta.Value; // } //} //else //{ // isLocal = true; // //NTAccount nta = (NTAccount)SID.Translate(typeof(NTAccount)); // memberName = nta.Value; //} } catch (Exception ex) { memberName = sid; isLocal = false; new NetSqlAzMan.Logging.LoggingUtility().WriteWarning(null, ex.Message + "\r\nSid: " + sid); } }
public static string SIDToAccountName(string sid) { try { SecurityIdentifier securityIdentifier; NTAccount account; if ((object)sid == null) throw new ArgumentNullException(nameof(sid)); securityIdentifier = new SecurityIdentifier(CleanSid(sid)); account = (NTAccount)securityIdentifier.Translate(typeof(NTAccount)); return account.ToString(); } catch (IdentityNotMappedException) { } catch (SystemException) { } return sid; }
public static IList<PeoplePickerSearchResultViewModel> FindUsers(string filter) { filter = filter.Trim(); filter = Regex.Replace(filter, "\\s+", " "); if (string.IsNullOrEmpty(filter)) { return new List<PeoplePickerSearchResultViewModel>(); } IList<PeoplePickerSearchResultViewModel> users = new List<PeoplePickerSearchResultViewModel>(); using (var directoryRoot = new DirectoryEntry(ConfigurationManager.AppSettings["ADPath"])) { var strSubFilter = ""; if (filter.Contains(',')) { var filterParts = filter.Split(','); var lastnameFilter = filterParts[0].Trim(); var firstnameFilter = filter.Substring(filter.IndexOf(',') + 1).Trim(); strSubFilter = string.Format("(&(givenName={0}*)(sn={1}*))(&(givenName={1}*)(sn={0}*))", firstnameFilter, lastnameFilter); } else if (filter.Contains(' ')) { var filterParts = filter.Split(' '); if (filterParts.Length == 2) { var firstnameFilter = filterParts[0].Trim(); var lastnameFilter = filterParts[1].Trim(); strSubFilter = string.Format("(&(givenName={0}*)(sn={1}*))(&(givenName={1}*)(sn={0}*))", firstnameFilter, lastnameFilter); } } var strFilter = string.Format( "(&(objectCategory=person)(objectClass=user)(|(cn={0}*)(sAMAccountName={0}*)(givenName={0}*)(sn={0}*){1}))", filter, strSubFilter); using (var directorySearcher = new DirectorySearcher(directoryRoot) { Filter = strFilter, SearchScope = SearchScope.Subtree, PageSize = 1000, Sort = new SortOption("cn", SortDirection.Ascending) }) { using (SearchResultCollection searchResults = directorySearcher.FindAll()) { foreach (SearchResult searchResult in searchResults) { string name = string.Empty; string login = string.Empty; string email = string.Empty; string telephone = string.Empty; string firstname = string.Empty; string lastname = string.Empty; int userAccountControl = 0; if (searchResult.Properties[AD_NAME].Count > 0) { name = searchResult.Properties[AD_NAME][0].ToString(); } if (searchResult.Properties[AD_FIRST_NAME].Count > 0) { firstname = searchResult.Properties[AD_FIRST_NAME][0].ToString(); } if (searchResult.Properties[AD_LAST_NAME].Count > 0) { lastname = searchResult.Properties[AD_LAST_NAME][0].ToString(); } if (searchResult.Properties[AD_LOGIN].Count > 0) { login = string.Format("SLI\\{0}", searchResult.Properties[AD_LOGIN][0].ToString().ToUpper()); } if (!login.StartsWith("xx", StringComparison.CurrentCultureIgnoreCase)) { if (searchResult.Properties[AD_SECURITY_ID].Count > 0) { try { var sid = new SecurityIdentifier( (byte[]) searchResult.Properties[AD_SECURITY_ID][0], 0); var account = (NTAccount) sid.Translate(typeof (NTAccount)); var username = account.ToString().ToUpper(); // This give the DOMAIN\User format for the account if (!string.IsNullOrEmpty(username) && username.Contains(login.ToUpper())) { login = username; } } catch { // do nothing } } users.Add(new PeoplePickerSearchResultViewModel { FirstName = firstname, LastName = lastname, Login = login }); } } } } } return users; }
internal static string GetNtAuthorityString() { if (Utils.NTAuthorityString == null) { SecurityIdentifier securityIdentifier = new SecurityIdentifier("S-1-5-18"); NTAccount nTAccount = (NTAccount)securityIdentifier.Translate(typeof(NTAccount)); int num = nTAccount.Value.IndexOf('\\'); Utils.NTAuthorityString = nTAccount.Value.Substring(0, num); } return Utils.NTAuthorityString; }
internal static string GetNtAuthorityString() { if (NTAuthorityString == null) { SecurityIdentifier identifier = new SecurityIdentifier("S-1-5-18"); NTAccount account = (NTAccount) identifier.Translate(typeof(NTAccount)); int index = account.Value.IndexOf('\\'); NTAuthorityString = account.Value.Substring(0, index); } return NTAuthorityString; }
internal static string GetNtAuthorityString() { if (s_NTAuthorityString == null) { SecurityIdentifier sidLocalSystem = new SecurityIdentifier("S-1-5-18"); NTAccount ntLocalSystem = (NTAccount)sidLocalSystem.Translate(typeof(NTAccount)); int index = ntLocalSystem.Value.IndexOf('\\'); Debug.Assert(index != -1); s_NTAuthorityString = ntLocalSystem.Value.Substring(0, index); } return s_NTAuthorityString; }
public static string GetLocalGroupString(string groupType) { int index = findIndexForGroupType(groupType); if (index < 0) throw new ArgumentException("groupType"); SecurityIdentifier sid = new SecurityIdentifier(sidGroupType[index]); NTAccount ntaccount = sid.Translate(typeof(NTAccount)) as NTAccount; string[] accountTokens = ntaccount.ToString().Split(new char[] { '\\' }); switch (accountTokens.Length) { case 2: return accountTokens[1]; case 1: return accountTokens[0]; default: throw new Exception("Account Token not in the known format- Time to research"); } }