private ActiveDirectoryUser SearchActiveDirectory(string samAccount, string domain) { if (string.IsNullOrWhiteSpace(domain)) { domain = Environment.UserDomainName; } // Store the search results as a collection of Users. This list will be returned. var user = new ActiveDirectoryUser(); user.SamId = samAccount; string ldapPath = $"LDAP://{domain}"; string searchFilter = "(&(objectCategory=person)(objectClass=user)" + $"(sAMAccountName={samAccount}))"; string[] searchProperties = { "givenName", "sn", "physicalDeliveryOfficeName", "telephoneNumber", "mail", "title", "department", "memberOf", "objectSid", "pwdLastSet", "distinguishedName" }; try { using ( GlobalVar.UseAlternateCredentials ? UserImpersonation.Impersonate(GlobalVar.AlternateUsername, GlobalVar.AlternateDomain, GlobalVar.AlternatePassword) : null) using (DirectoryEntry parentEntry = new DirectoryEntry(ldapPath)) using (DirectorySearcher directorySearcher = new DirectorySearcher(parentEntry, searchFilter, searchProperties)) using (SearchResultCollection searchResultCollection = directorySearcher.FindAll()) { // Iterate through each search result. foreach (SearchResult searchResult in searchResultCollection) { DirectoryEntry entry = new DirectoryEntry(searchResult.GetDirectoryEntry().Path); user.NameFirst = (entry.Properties["givenName"].Value != null) ? entry.Properties["givenName"].Value.ToString() : string.Empty; user.NameLast = (entry.Properties["sn"].Value != null) ? entry.Properties["sn"].Value.ToString() : string.Empty; user.OfficeLocation = (entry.Properties["physicalDeliveryOfficeName"].Value != null) ? entry.Properties["physicalDeliveryOfficeName"].Value.ToString() : string.Empty; user.OfficePhone = (entry.Properties["telephoneNumber"].Value != null) ? entry.Properties["telephoneNumber"].Value.ToString() : string.Empty; user.JobDepartment = (entry.Properties["department"].Value != null) ? entry.Properties["department"].Value.ToString() : string.Empty; user.JobTitle = (entry.Properties["title"].Value != null) ? entry.Properties["title"].Value.ToString() : string.Empty; user.EmailAddress = (entry.Properties["mail"].Value != null) ? entry.Properties["mail"].Value.ToString() : string.Empty; if (entry.Properties["objectSid"].Value != null) { user.SID = new SecurityIdentifier((byte[])entry.Properties["objectSid"].Value, 0).ToString(); } if (entry.Properties["memberOf"] != null) { user.MemberOf = entry.Properties["memberOf"]; } //user.PasswordLastSet = (entry.Properties["pwdLastSet"].Value != null) ? entry.Properties["pwdLastSet"].Value.ToString() : string.Empty; for (int i = 0; i < user.MemberOf.Count; ++i) { int startIndex = user.MemberOf[i].ToString().IndexOf("CN=", 0) + 3; //+3 for length of "OU=" int endIndex = user.MemberOf[i].ToString().IndexOf(",", startIndex); user.MemberOf[i] = user.MemberOf[i].ToString().Substring((startIndex), (endIndex - startIndex)); } user.DistinguishedName = entry.Properties["distinguishedName"].Value.ToString(); string dn = entry.Properties["distinguishedName"].Value.ToString(); int dnStartIndex = dn.IndexOf(",", 1) + 4; //+3 for length of "OU=" int dnEndIndex = dn.IndexOf(",", dnStartIndex); user.OrganizationalUnit = dn.Substring((dnStartIndex), (dnEndIndex - dnStartIndex)); } } } catch { } return(user); }
private void btnSave_Click(object sender, RoutedEventArgs e) { const string odbcRoot = @"SOFTWARE\ODBC\ODBC.INI"; const string odbcRoot32bitOn64bit = @"SOFTWARE\Wow6432Node\ODBC\ODBC.INI"; const string serviceName = "RemoteRegistry"; bool isLocal = RemoteSystemInfo.TargetComputer.ToUpper() == Environment.MachineName.ToUpper() ? true : false; bool isServiceRunning = true; // If the target computer is remote, then start the Remote Registry service. using ( GlobalVar.UseAlternateCredentials ? UserImpersonation.Impersonate(GlobalVar.AlternateUsername, GlobalVar.AlternateDomain, GlobalVar.AlternatePassword) : null) using (var sc = new ServiceController(serviceName, RemoteSystemInfo.TargetComputer)) { try { if (!isLocal && sc.Status != ServiceControllerStatus.Running) { isServiceRunning = false; sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); } } catch { } try { using (RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, RemoteSystemInfo.TargetComputer)) { if (_OdbcDsn.Is32bitOn64bit) { using (RegistryKey subKey = key.OpenSubKey($@"{odbcRoot32bitOn64bit}\{_OdbcDsn.DataSourceName}", true)) { if (subKey != null) { subKey.SetValue(_OdbcValue.OdbcValueName, txtOdbcNewValue.Text); } } } else { using (RegistryKey subKey = key.OpenSubKey($@"{odbcRoot}\{_OdbcDsn.DataSourceName}", true)) { if (subKey != null) { subKey.SetValue(_OdbcValue.OdbcValueName, txtOdbcNewValue.Text); } } } } } catch { } // Cleanup. if (!isLocal && !isServiceRunning) { try { if (sc != null) { sc.Stop(); } } catch (Exception) { } } } this.Close(); }