private void moveOUButton_Click(object sender, EventArgs e) { try { PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, The_Admin_Toolbox.TheAdminToolBox.domain); //Create a "user object" in the context UserPrincipal user = new UserPrincipal(domainContext); //Specify the search parameters bool fHasSpace = adtext.Contains(" "); if (fHasSpace) { string[] ssize = adtext.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string first = ssize[0]; string last = ssize[1]; user.GivenName = first; user.Surname = last; } else { user.SamAccountName = adtext; } //Create the searcher //pass (our) user object PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = user; //Perform the search PrincipalSearchResult <Principal> results = pS.FindAll(); //If necessary, request more details Principal pc = results.ToList()[0]; DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject(); //Output first result of the test // try // { //Gets SamAcctName string sam = pc.SamAccountName.ToString(); MessageBox.Show(pc.DistinguishedName); DirectoryEntry usermove = new DirectoryEntry(@"LDAP://" + pc.DistinguishedName); usermove.MoveTo(new DirectoryEntry(@"LDAP://" + OUcomboBox.Text)); MessageBox.Show(adtext + " was moved to " + OUcomboBox.Text + " successfully."); this.Close(); } catch (Exception err) { System.Windows.Forms.MessageBox.Show(err.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); this.Close(); } }
private void buttonRemove_Click(object sender, EventArgs e) { //Create a shortcut to the appropriate Windows domain PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain); //Create a "user object" in the context UserPrincipal user = new UserPrincipal(domainContext); PrincipalContext localContext = new PrincipalContext(ContextType.Machine, computername); //Check if it's the SamAccountName or if it's first name and last name string adtext = textBoxUser.Text; bool fHasSpace = adtext.Contains(" "); if (fHasSpace) { string[] ssize = adtext.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string first = ssize[0]; string last = ssize[1]; user.GivenName = first; user.Surname = last; } else { user.SamAccountName = adtext; } PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = user; //Perform the search try { //Remove user from local admin group PrincipalSearchResult <Principal> results = pS.FindAll(); Principal pc = results.ToList()[0]; string sam = pc.SamAccountName; DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + computername); DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group"); admGroup.Invoke("Remove", "WinNT://" + domain + "/" + sam + ",user"); admGroup.CommitChanges(); admGroup.Dispose(); admGroup.Close(); localMachine.Close(); System.Windows.Forms.MessageBox.Show("User has been removed!", "Removing user from local admins group", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.Close(); } catch (SystemException err) { System.Windows.Forms.MessageBox.Show(err.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); this.Close(); } }
public ActionResult Details(int id) { SelectList list = new SelectList(db.Teams.ToList(), "TeamId", "Name"); ViewBag.Teams = list; PrincipalSearchResult <Principal> groups = interaction.GetUserGroups(); ViewBag.UserGroup = new SelectList(groups.ToList(), "Name", "Name"); return(View(db.Students.Find(id))); }
static public string[] GetGroupNames(string domainName, string userName) { List <string> result = new List <string>(); using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName)) { using (PrincipalSearchResult <Principal> src = UserPrincipal.FindByIdentity(principalContext, userName).GetGroups()) { src.ToList().ForEach(sr => result.Add(sr.SamAccountName)); } } return(result.ToArray()); }
private IList <Principal> GetUserAuthorizationGroups(string username) { var ctx = GeneratePrincipalContext(); var up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username); if (up != null) { PrincipalSearchResult <Principal> authGroups = up.GetAuthorizationGroups(); return(authGroups.ToList()); } return(new List <Principal>()); }
public static bool AuthenticateUser(string apiUser) { string username = ConfigurationManager.AppSettings["AllowedUsers"].ToString(); string[] users = username.Split(';'); if (users.Contains(apiUser)) { var context = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain, "ES-Area1"); var searchPrinciple = new UserPrincipal(context); searchPrinciple.SamAccountName = apiUser; PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = searchPrinciple; //searches based on SamAccountName or DisplayName etc. //Perform the search PrincipalSearchResult <Principal> results = pS.FindAll(); if (results.ToList().Count > 0) { Principal pc = results.ToList()[0]; DirectoryEntry direEn = (DirectoryEntry)pc.GetUnderlyingObject(); Console.WriteLine("Email ID: " + direEn.Properties["mail"].Value.ToString()); Console.WriteLine("-------------------------------------------"); Console.WriteLine("First Name: " + direEn.Properties["givenName"].Value); Console.WriteLine("-------------------------------------------"); Console.WriteLine("Last Name : " + direEn.Properties["sn"].Value); Console.WriteLine("-------------------------------------------"); Console.WriteLine("SAM account name : " + direEn.Properties["samAccountName"].Value); Console.WriteLine("-------------------------------------------"); Console.WriteLine("User principal name: " + direEn.Properties["userPrincipalName"].Value); Console.WriteLine("-------------------------------------------"); Console.WriteLine(); return(true); } } return(false); }
public ActionResult Create() { Student student = new Student(); Credentials cred = new Credentials(); student.Credentials = cred; ViewBag.StartPort = (db.Credentials.Max(x => x.WebsitePort) + 1).ToString(); SelectList list = new SelectList(db.Teams.ToList(), "TeamId", "Name"); ViewBag.Teams = list; PrincipalSearchResult <Principal> groups = interaction.GetUserGroups(); ViewBag.UserGroup = new SelectList(groups.ToList(), "Name", "Name"); return(View(student)); }
public IEnumerable <string> GetRoles(string userName, RoleTypes roleType) { var returnedRoles = new List <string>(); using (PrincipalContext context = GetPrincipalContext()) { // find the user in the identity store UserPrincipal user = UserPrincipal.FindByIdentity(context, userName); // get the groups for the user principal and // store the results in a PrincipalSearchResult object PrincipalSearchResult <Principal> results = user.GetGroups(); results.ToList().ForEach(result => returnedRoles.Add(result.Name)); } return(returnedRoles); }
private IList <Principal> GetUserAuthorizationGroups(string username) { var ctx = GeneratePrincipalContext(); var up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username); if (useLocalMachine == false && up == null) { //we can't find the UserPrincipal inside the domain //we need to look for it in the local machine ctx = new PrincipalContext(ContextType.Machine); up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username); } if (up == null) { return(new List <Principal>()); } PrincipalSearchResult <Principal> authGroups = up.GetAuthorizationGroups(); return(authGroups.ToList()); }
private void buttonSubmit_Click(object sender, EventArgs e) { //Create a shortcut to the appropriate Windows domain PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, addomain); //Create a "user object" in the context UserPrincipal user = new UserPrincipal(domainContext); //Specify the search parameters bool fHasSpace = adtext.Contains(" "); if (fHasSpace) { string[] ssize = adtext.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string first = ssize[0]; string last = ssize[1]; user.GivenName = first; user.Surname = last; } else { user.SamAccountName = adtext; } //Create the searcher //pass (our) user object PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = user; //Perform the search PrincipalSearchResult <Principal> results = pS.FindAll(); //If necessary, request more details //Output first result of the test try { Principal pc = results.ToList()[0]; DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject(); //Gets SamAcctName string sam = pc.SamAccountName.ToString(); //Finds account using sam UserPrincipal usr = UserPrincipal.FindByIdentity(domainContext, sam); //Test to see if account is locked, if it is unlocks account string password = this.textBoxPass.Text; if (usr.IsAccountLockedOut()) { usr.UnlockAccount(); usr.SetPassword(password); pc.Dispose(); //ResetPass.ActiveForm.Close(); long filetime = TheAdminToolBox.ConvertADSLargeIntegerToInt64(de.Properties["pwdLastSet"].Value); DateTime pwdSet = DateTime.FromFileTime(filetime); System.Windows.Forms.MessageBox.Show("Account is now unlocked" + "\r\nPassword has been changed.", "Password set", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { usr.SetPassword(password); de.CommitChanges(); long filetime = TheAdminToolBox.ConvertADSLargeIntegerToInt64(de.Properties["pwdLastSet"].Value); DateTime pwdSet = DateTime.FromFileTime(filetime); System.Windows.Forms.MessageBox.Show("Password has been changed.", "Password set", MessageBoxButtons.OK, MessageBoxIcon.Information); de.Close(); pc.Dispose(); this.Close(); } pS.Dispose(); } catch (SystemException err) { System.Windows.Forms.MessageBox.Show(err.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); this.Close(); } }
public ResetPass() { InitializeComponent(); this.Text = "AD Password Reset"; this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); if (!(String.IsNullOrEmpty(labelPass.Text))) { labelPass.Text = "Resetting password for " + adtext; } //MessageBox.Show("Resetting password for " + adtext + " on " + addomain + " domain"); //Create a shortcut to the appropriate Windows domain PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, addomain); //Create a "user object" in the context UserPrincipal user = new UserPrincipal(domainContext); //Specify the search parameters bool fHasSpace = adtext.Contains(" "); if (fHasSpace) { string[] ssize = adtext.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string first = ssize[0]; string last = ssize[1]; user.GivenName = first; user.Surname = last; } else { user.SamAccountName = adtext; } //Create the searcher //pass (our) user object PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = user; //Perform the search PrincipalSearchResult <Principal> results = pS.FindAll(); //If necessary, request more details //Output first result of the test try { Principal pc = results.ToList()[0]; DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject(); //Gets SamAcctName string sam = pc.SamAccountName.ToString(); //Finds account using sam UserPrincipal usr = UserPrincipal.FindByIdentity(domainContext, sam); //Checks to see if the user has ever logged in before, if they haven't they must change their password upon next logon if (usr.LastPasswordSet.HasValue == false && usr.PasswordNeverExpires == false) { mustChangeCheckbox.CheckState = CheckState.Checked; } else { mustChangeCheckbox.CheckState = CheckState.Unchecked; } pS.Dispose(); } catch (SystemException err) { System.Windows.Forms.MessageBox.Show(err.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); this.Close(); } }
private void mustChangeCheckbox_Click(object sender, EventArgs e) { //Create a shortcut to the appropriate Windows domain PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, addomain); //Create a "user object" in the context UserPrincipal user = new UserPrincipal(domainContext); //Specify the search parameters bool fHasSpace = adtext.Contains(" "); if (fHasSpace) { string[] ssize = adtext.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string first = ssize[0]; string last = ssize[1]; user.GivenName = first; user.Surname = last; } else { user.SamAccountName = adtext; } //Create the searcher //pass (our) user object PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = user; //Perform the search PrincipalSearchResult <Principal> results = pS.FindAll(); try { //If necessary, request more details Principal pc = results.ToList()[0]; DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject(); //Gets SamAcctName string sam = pc.SamAccountName.ToString(); //Finds account using sam UserPrincipal usr = UserPrincipal.FindByIdentity(domainContext, sam); //Output first result of the test if (mustChangeCheckbox.Checked == false) { if (!(object.ReferenceEquals(null, de.Properties["pwdLastSet"].Value))) { de.Properties["pwdLastSet"].Value = -1; de.CommitChanges(); System.Windows.Forms.MessageBox.Show("User must change password at next logon was removed!"); } } if (mustChangeCheckbox.Checked == true) { if (!(object.ReferenceEquals(null, de.Properties["pwdLastSet"].Value))) { usr.ExpirePasswordNow(); System.Windows.Forms.MessageBox.Show("User must change password at next logon is now set!"); } } } catch (SystemException err) { System.Windows.Forms.MessageBox.Show(err.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); this.Close(); } }
/// <summary> /// Wandelt Principal in DirectoryEntry um /// </summary> /// <param name="results"></param> /// <returns></returns> public List <DirectoryEntry> ConvertPrincipalsToDirectoryEntries(PrincipalSearchResult <Principal> results) { return(( //Umwandlung Principal->DirectoryEntry results.ToList().Cast <Principal>().Select(pc => (DirectoryEntry)pc.GetUnderlyingObject())).ToList()); }
public static int AddUserByUID(string strUser, Int32 intAPPID) { Int32 intASID = 0; RexrothEntities db = clsStart.efdbRexroth(); string strDomain = ""; System.Data.Entity.Core.Objects.ObjectParameter pk = new System.Data.Entity.Core.Objects.ObjectParameter("PK", typeof(int)); System.Data.Entity.Core.Objects.ObjectParameter error = new System.Data.Entity.Core.Objects.ObjectParameter("EMessage", typeof(string)); Cursor.Current = Cursors.WaitCursor; var lstDomains = new string[] { "US", "DE", "MX" }; if (!(UserExists(strUser))) { foreach (string strDom in lstDomains) { try { //var qry = (from ct in db.tblApp // where ct.APPID == intAPPID // select ct).FirstOrDefault(); PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, strDom); UserPrincipal user = new UserPrincipal(domainContext); //Specify the search parameters user.Name = strUser; PrincipalSearcher pS = new PrincipalSearcher(); pS.QueryFilter = user; PrincipalSearchResult <Principal> results = pS.FindAll(); //If necessary, request more details //Principal pc = results.ToList()[0]; //DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject(); if (results.ToList().Count > 0) { foreach (UserPrincipal usr in results) { //tblAssociate tbl = new tblAssociate(); //ObjectParameter name = new ObjectParameter("Name", typeof(String)); db.p_SaveChangesEF(usr.GivenName, usr.Surname, usr.SamAccountName, usr.EmailAddress.ToString(), usr.DisplayName, pk, error); if (Convert.ToInt32(pk.Value) == 0) { MessageBox.Show(error.ToString()); } else { Cursor.Current = Cursors.Default; MessageBox.Show(usr.DisplayName + " added!", "Credo"); } } } else { Cursor.Current = Cursors.Default; MessageBox.Show(strUser + " not found.", "Credo"); } } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show(ex.Message, "Credo"); } } } else { Cursor.Current = Cursors.Default; MessageBox.Show(strUser + " already exists.", "Credo"); } Cursor.Current = Cursors.Default; intASID = Convert.ToInt32(pk.Value); return(intASID); }