public ADUserDetail GetUserByLoginName(String userName) { try { using (HostingEnvironment.Impersonate()) { // This code runs as the application pool user _directoryEntry = null; string nn = "LDAP://PRIME.local/DC=PRIME,DC=local"; DirectoryEntry SearchRoot2 = new DirectoryEntry(nn); DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); directorySearch.Filter = "(&(objectClass=user)(SAMAccountName=" + userName + "))"; SearchResult results = directorySearch.FindOne(); if (results != null) { DirectoryEntry user = new DirectoryEntry(results.Path); //, LDAPUser, LDAPPassword); return(ADUserDetail.GetUser(user)); } return(null); } } catch (Exception ex) { return(null); } }
internal ADUserDetail GetUserByFullName(String userName) { try { using (HostingEnvironment.Impersonate()) { _directoryEntry = null; DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); directorySearch.Filter = "(&(objectClass=user)(cn=" + userName + "))"; SearchResult results = directorySearch.FindOne(); if (results != null) { DirectoryEntry user = new DirectoryEntry(results.Path); // LDAPUser, LDAPPassword); return(ADUserDetail.GetUser(user)); } else { return(null); } } } catch (Exception ex) { return(null); } }
public List <ADUserDetail> GetUsersByFirstName(string fName) { using (HostingEnvironment.Impersonate()) { //UserProfile user; List <ADUserDetail> userlist = new List <ADUserDetail>(); string filter = ""; _directoryEntry = null; DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); directorySearch.Asynchronous = true; directorySearch.CacheResults = true; filter = string.Format("(givenName={0}*", fName); // filter = "(&(objectClass=user)(objectCategory=person)(givenName="+fName+ "*))"; directorySearch.Filter = filter; SearchResultCollection userCollection = directorySearch.FindAll(); foreach (SearchResult users in userCollection) { DirectoryEntry userEntry = new DirectoryEntry(users.Path); //, LDAPUser, LDAPPassword); ADUserDetail userInfo = ADUserDetail.GetUser(userEntry); userlist.Add(userInfo); } directorySearch.Filter = "(&(objectClass=group)(SAMAccountName=" + fName + "*))"; SearchResultCollection results = directorySearch.FindAll(); if (results != null) { foreach (SearchResult r in results) { DirectoryEntry deGroup = new DirectoryEntry(r.Path); //, LDAPUser, LDAPPassword); ADUserDetail agroup = ADUserDetail.GetUser(deGroup); userlist.Add(agroup); } } return(userlist); } }
private bool execADCmd() { bool Disabled = (!cbEnabled.Checked); if (adHelper.OU != tbOU.Text) { adHelper.OU = tbOU.Text.Trim(); } ADUserDetail user = new ADUserDetail(tbUser.Text, Disabled, tbFirst.Text, tbLast.Text, tbEmail.Text, tbPwd.Text); string cmd = (string)cbCommand.SelectedItem; string err = ""; switch (cmd.ToLower()) { case "add": adHelper.AddUserByLogin(user, ref err); break; case "delete": adHelper.DeleteUserByLogin(user, ref err); break; case "disable": adHelper.DisableUserByLogin(user, ref err); break; case "enable": adHelper.EnableUserByLogin(user, ref err); break; case "import": break; case "update": adHelper.UpdateUserByLogin(user, false, ref err); break; case "list": List <ADUserDetail> userList = adHelper.GetUsers(); if (userList.Count <= 0) { rtbMsg.AppendText("No users found."); return(false); } foreach (ADUserDetail u in userList) { rtbMsg.AppendText("Username: "******", Disabled=" + u.AccountDisabled.ToString() + ", FirstName=" + u.FirstName + ",LastName=" + u.LastName + ", Email=" + u.EmailAddress + "\r\n"); rtbMsg.AppendText("===================================================================================================================\r\n"); } break; default: MessageBox.Show("Unknown command."); return(false); } rtbMsg.AppendText("Successfully completed " + cmd + "!"); return(true); }
public DiagnosticDTO Get() { Dictionary <string, string> qs = HttpRequestHelper.GetQueryStrings(Request); WritetoTrace = qs.ContainsKey("trace"); DiagnosticDTO ret = TestConnection(this.User.Identity.Name.Substring(this.User.Identity.Name.LastIndexOf(@"\") + 1)); ret.UserName = this.User.Identity.Name.Substring(this.User.Identity.Name.LastIndexOf(@"\") + 1); ret.Environment = AppSettings.Instance.Environment; //ret.EzConnectionString = LinqSQLHelper.RemoveConnectionStringSecurity(AppSettings.Instance.ConnectionString); ret.EzConnectionString = AppSettings.Instance.ConnectionString; ret.Host = AppSettings.Instance.HostName; ret.ComputerName = System.Net.Dns.GetHostName().ToLower(); ret.UserName = this.User.Identity.Name.Substring(this.User.Identity.Name.LastIndexOf(@"\") + 1); ret.DisableSecurity = false; ret.TraceEnable = false; ret.Role = "N/A"; if (ret.DiagInfo == null) { ret.DiagInfo = ""; } ret.DiagInfo += string.Format("Auth Info: AuthenticationType={0}, IsAuthenticated={1}, Name={2}, ImpersonationLevel={3}, IsAnonymous={4}", this.User.Identity.AuthenticationType, this.User.Identity.IsAuthenticated, this.User.Identity.Name, ((System.Security.Principal.WindowsIdentity) this.User.Identity).ImpersonationLevel, ((System.Security.Principal.WindowsIdentity) this.User.Identity).IsAnonymous); try { if (!AppSettings.Instance.Environment.ToLower().StartsWith("local")) { ActiveDirectoryHelper ad = new ActiveDirectoryHelper(); ADUserDetail userDetail = ad.GetUserByLoginName(ret.UserName); List <string> roles = ad.GetUserGroupMembership(ret.UserName); if (userDetail != null) { ret.FullNameFromAD = userDetail.FirstName + " " + userDetail.LastName; } } else { ret.FullNameFromAD = "N/A in LOCAL"; } } catch (Exception ex) { ret.FullNameFromAD = "N/A in LOCAL... From Error:\n" + ex.ToString(); } return(ret); }
public ADUserDetail GetUserDetailsByFullName(String FirstName, String MiddleName, String LastName) { //givenName // initials // sn //(initials=" + MiddleName + ")(sn=" + LastName + ") try { using (HostingEnvironment.Impersonate()) { _directoryEntry = null; DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); //directorySearch.Filter = "(&(objectClass=user)(givenName=" + FirstName + ") ())"; if (FirstName != "" && MiddleName != "" && LastName != "") { directorySearch.Filter = "(&(objectClass=user)(givenName=" + FirstName + ")(initials=" + MiddleName + ")(sn=" + LastName + "))"; } else if (FirstName != "" && MiddleName != "" && LastName == "") { directorySearch.Filter = "(&(objectClass=user)(givenName=" + FirstName + ")(initials=" + MiddleName + "))"; } else if (FirstName != "" && MiddleName == "" && LastName == "") { directorySearch.Filter = "(&(objectClass=user)(givenName=" + FirstName + "))"; } else if (FirstName != "" && MiddleName == "" && LastName != "") { directorySearch.Filter = "(&(objectClass=user)(givenName=" + FirstName + ")(sn=" + LastName + "))"; } else if (FirstName == "" && MiddleName != "" && LastName != "") { directorySearch.Filter = "(&(objectClass=user)(initials=" + MiddleName + ")(sn=" + LastName + "))"; } SearchResult results = directorySearch.FindOne(); if (results != null) { DirectoryEntry user = new DirectoryEntry(results.Path); //, LDAPUser, LDAPPassword); return(ADUserDetail.GetUser(user)); } return(null); } } catch (Exception ex) { return(null); } }
public bool DisableUser(User user, int requestor, int reqId, ref string err) { err = ""; bool logErr = false; try { if (CheckData(user, ref err) == false) { int Event_id = 6035; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); return(false); } ADUserDetail aduser = new ADUserDetail(user.Username, !user.Enabled, user.Firstname, user.Lastname, user.Email); bool status = m_adHelper.DisableUserByLogin(aduser, ref err); if (status == true) { err = ""; } else { err += ", OU " + m_OU_Filter; int Event_id = 6036; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } return(status); } catch (Exception ex) { err = "Failed to disable the user account, username="******", OU " + m_OU_Filter + ", Exception:" + ex.Message; int Event_id = 6037; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); return(false); } finally { if (logErr) { int Event_id = 6038; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } } }
static void Main(string[] args) { string line; StreamReader fsEmpId = new StreamReader(@"C:\Users\143968\Desktop\EmpId.txt"); ActiveDirectoryHelper.ActiveDirectoryHelper adh = new ActiveDirectoryHelper.ActiveDirectoryHelper(); while ((line = fsEmpId.ReadLine()) != null) { ADUserDetail userDetail = adh.GetUserByLoginName(line); if (userDetail.Manager != null) { Console.WriteLine(userDetail.LoginName + "," + userDetail.Manager.LoginName); } else { Console.WriteLine(userDetail.LoginName + "," + userDetail.ManagerName); } } Console.ReadLine(); }
/// <summary> /// This function will take a DL or Group name and return list of users /// </summary> /// <param name="groupName"></param> /// <returns></returns> public List <ADUserDetail> GetUserFromGroup(String groupName) { List <ADUserDetail> userlist = new List <ADUserDetail>(); try { using (HostingEnvironment.Impersonate()) { _directoryEntry = null; DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot); directorySearch.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))"; SearchResult results = directorySearch.FindOne(); if (results != null) { DirectoryEntry deGroup = new DirectoryEntry(results.Path); //, LDAPUser, LDAPPassword); System.DirectoryServices.PropertyCollection pColl = deGroup.Properties; int count = pColl["member"].Count; for (int i = 0; i < count; i++) { string respath = results.Path; string[] pathnavigate = respath.Split("CN".ToCharArray()); respath = pathnavigate[0]; string objpath = pColl["member"][i].ToString(); string path = respath + objpath; DirectoryEntry user = new DirectoryEntry(path); //, LDAPUser, LDAPPassword); ADUserDetail userobj = ADUserDetail.GetUser(user); userlist.Add(userobj); user.Close(); } } return(userlist); } } catch (Exception ex) { return(userlist); } }
protected void txtUserName_TextChanged(object sender, EventArgs e) { if (txtUserName.Text != "") { if (DoesUserExist(txtUserName.Text) == true) { try { UserInfo = new ActiveDirectoryUtils.ActiveDirectoryUtil().GetUserByLoginName(txtUserName.Text.Trim()); lblUserIdDsp.Text = UserInfo.Company; staffBranch = UserInfo.PhysicalDeliveryOfficename; Session["staffBranch"] = staffBranch; staffFirstname = UserInfo.FirstName; Session["staffFirstname"] = staffFirstname; staffSurname = UserInfo.LastName; Session["staffSurname"] = staffSurname; string UsernameTextInput = ""; UsernameTextInput = Server.HtmlEncode(txtUserName.Text).ToLower(); lblMsg.Text = ""; Um.SelectExistingUserRole(UsernameTextInput); string userName = Um.Username.ToString().ToLower(); string userRoles = Um.UserRole.ToString().ToLower(); if (userName != "") { if (userName == UsernameTextInput && userRoles == "iscontrol") { RadioButtonList1.SelectedIndex = 2; } else if (userName == UsernameTextInput && userRoles == "audit") { RadioButtonList1.SelectedIndex = 1; } else if (userName == UsernameTextInput && userRoles == "rco") { RadioButtonList1.SelectedIndex = 0; } } else { lblMsg.Text = "New user!"; lblMsg.ForeColor = System.Drawing.Color.Red; RadioButtonList1.SelectedIndex = -1; } } catch (Exception) { if (lblUserIdDsp.Text != "") { lblMsg.Text = "This user has not been profiled yet! "; lblMsg.ForeColor = System.Drawing.Color.Red; RadioButtonList1.SelectedIndex = -1; } else { lblMsg.Text = "User does not exist!"; lblUserIdDsp.Text = ""; lblMsg.ForeColor = System.Drawing.Color.Red; RadioButtonList1.SelectedIndex = -1; } } } else if (DoesUserExist(txtUserName.Text) == false) { lblMsg.Text = "This user does not exist!"; lblUserIdDsp.Text = ""; lblMsg.ForeColor = System.Drawing.Color.Red; RadioButtonList1.SelectedIndex = -1; } } else { lblMsg.Text = "Please enter Username!"; lblMsg.ForeColor = System.Drawing.Color.Red; } }
public bool ChangeUserPassword(string login, string pwd, int requestor, int reqId, ref string err) { err = ""; bool logErr = false; bool fatal = false; try { if (String.IsNullOrEmpty(login)) { err = "ChangeUserPassword, Username is empty, OU=" + m_OU_Filter; int Event_id = 6021; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); return(false); } if (String.IsNullOrEmpty(pwd)) { err = "ChangeUserPassword, Password is empty, OU=" + m_OU_Filter; int Event_id = 6022; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); return(false); } if (reqId <= 0) { err = "ChangeUserPassword, RequestId must be greater than 0, reqId=" + reqId.ToString() + ", OU=" + m_OU_Filter; int Event_id = 6023; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); return(false); } if (requestor <= 0) // if there is no user id of the requestor is not given { requestor = 0; } User user = m_dataAccess.GetUserAccount(login, ref err); if (user == null) { err = "ChangeUserPassword, err: " + err; return(false); } if ((user.Firstname == string.Empty) || (user.Lastname == string.Empty)) { err = "ChangeUserPassword, username="******" missing First Name or Last Name in the database."; return(false); } //if (user.Password == string.Empty) //{ user.Password = pwd; //} ADUserDetail adUser = new ADUserDetail(user.Username, !user.Enabled, user.Firstname, user.Lastname, user.Email, user.Password); bool status = m_adHelper.ChangeUserPassword(adUser, ref err); if (status == false) { err += ", OU " + m_OU_Filter; int Event_id = 6024; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } else { err = ""; } return(status); } catch (Exception ex) { //UpdateRequest(requestor, reqId, UserMgrCommandType.ChangePassword, false, ex.Message, ref err); err = "Failed to ChangeUserPassword the user account, username="******", OU " + m_OU_Filter + ", Exception:" + ex.Message; int Event_id = 6025; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); return(false); } finally { if (logErr) { int Event_id = 6026; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } } }
private void ProcessRequest(object o) { bool logErr = false; bool fatal = false; bool status = false; string err = ""; ADUserDetail adUser = null; UserManagerEvent e = o as UserManagerEvent; if (e == null) { err = "ProcessRequest, UserManagerEvent argument is empty, OU " + m_OU_Filter; int Event_id = 6012; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } if (e.UserId <= 0) { e.UserId = 0; } if (e.RequestType == Common.UserMgrCommandType.Unknown) { err = "UserManagerEvent RequestType is Unknown, ReqType=" + e.RequestType; int Event_id = 6014; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } if (e.RequestId <= 0) { err = "UserManagerEvent RequestId is invalid, RequestId=" + e.RequestId.ToString(); int Event_id = 6015; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } try { if ((e.RequestType != Common.UserMgrCommandType.ImportUsers) && (e.RequestType != Common.UserMgrCommandType.StartImport) && (e.RequestType != Common.UserMgrCommandType.StopImport)) { if (e.ADUser == null) { err = "Received invalid UserManagerEvent RequestType, ReqType=" + e.RequestType + ", OU=" + m_OU_Filter; int Event_id = 6016; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } adUser = new ADUserDetail(e.ADUser.Username, !e.ADUser.Enabled, e.ADUser.Firstname, e.ADUser.Lastname, e.ADUser.Email, e.ADUser.Password); } if (SetUpADHelper(ref err) == false) { int Event_id = 6017; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } switch (e.RequestType) { case Common.UserMgrCommandType.CreateUser: status = m_adHelper.AddUserByLogin(adUser, false, ref err); break; case Common.UserMgrCommandType.DeleteUser: status = m_adHelper.DeleteUserByLogin(adUser, ref err); break; case Common.UserMgrCommandType.DisableUser: status = m_adHelper.DisableUserByLogin(adUser, ref err); break; case Common.UserMgrCommandType.EnableUser: status = m_adHelper.EnableUserByLogin(adUser, ref err); break; case Common.UserMgrCommandType.ImportUsers: status = ImportADUsers(e.UserId, e.RequestId, ref err); break; case Common.UserMgrCommandType.StartImport: //status = StartImport(); break; case Common.UserMgrCommandType.StopImport: //status = StopImport(); break; } if (status == false) { int Event_id = 6018; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } } catch (Exception ex) { err = "Failed to " + e.RequestType.ToString() + ", OU " + m_OU_Filter + ", Exception:" + ex.Message; int Event_id = 6019; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } finally { if (logErr) { int Event_id = 6020; EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category); } string errMsg = ""; UpdateRequest(e.UserId, e.RequestId, e.RequestType, status, err, ref errMsg); } }