public User(UserPrincipalExt _user) { try { var user = (UserPrincipalExt)_user; name = user.GivenName ?? "?"; surname = user.Surname ?? "?"; fullname = name + " " + surname ?? "?"; sicil = user.SamAccountName ?? "?"; phone = user.VoiceTelephoneNumber ?? "?"; mobile = user.ExtensionAttribute11 ?? "?"; //mail = user.EmailAddress ?? "?"; } catch (InvalidDataException ee) { Console.Write("\n | " + ee.Message); } }
/// <summary> /// /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <param name="grp"></param> /// <returns></returns> static public List <User> extractUsers(int start, int end, List <string> members) { Console.Write("\n"); List <User> users = new List <User>(); Color myC = start > 0 ? Color.Cyan : Color.Yellow; UserPrincipalExt extUser; for (int x = start; x < end; x++) { if (start > 0) { Console.Write("\r User " + x + " "); } try { extUser = UserPrincipalExt.FindByIdentity(principalContext, IdentityType.SamAccountName, members[x]); users.Add(new User(extUser)); } catch (Exception ee) { Console.WriteFormatted("\n\t| Exception user" + x + "(" + members[x] + "): " + ee.Message + "\n", Color.Red); } } return(users); }
/// <summary> /// Creates a new user /// </summary> /// <param name="user"></param> /// <param name="baseOrganizationalUnit"></param> /// <param name="isUsingDisplayNameAsNameAttribute"></param> public UsersObject NewUser(UsersObject user, string companyUsersPath, bool isUsingDisplayNameAsNameAttribute) { PrincipalContext pc = null; UserPrincipalExt up = null; try { pc = new PrincipalContext(ContextType.Domain, this.domainController, companyUsersPath, this.username, this.password); logger.Debug("Looking to see if user already exists: " + user.UserPrincipalName); bool doesExist = DoesUserPrincipalNameExist(user.UserPrincipalName); if (doesExist) throw new Exception("User already exists"); else { // Find an available sAMAccountName user.sAMAccountName = GetAvailableSamAccountName(user.UserPrincipalName); // User was not found so lets create the new user up = new UserPrincipalExt(pc, user.sAMAccountName, user.Password, true); up.UserPrincipalName = user.UserPrincipalName; up.DisplayName = user.DisplayName; up.PasswordNeverExpires = user.PasswordNeverExpires; if (isUsingDisplayNameAsNameAttribute) up.Name = user.DisplayName; else up.Name = user.UserPrincipalName; if (!string.IsNullOrEmpty(user.Firstname)) up.GivenName = user.Firstname; if (!string.IsNullOrEmpty(user.Middlename)) up.MiddleName = user.Middlename; if (!string.IsNullOrEmpty(user.Lastname)) up.LastName = user.Lastname; if (!string.IsNullOrEmpty(up.Department)) up.Department = user.Department; up.Save(); // Get the user's GUID user.UserGuid = (Guid)up.Guid; // Get the user's distinguished name user.DistinguishedName = up.DistinguishedName; // Return the user with the information return user; } } catch (Exception ex) { this.logger.Error("Error creating new user " + user.UserPrincipalName, ex); throw; } finally { if (up != null) up.Dispose(); if (pc != null) pc.Dispose(); } }
public User Create(string usersOU, string clearTextPassword, User userObject) { PrincipalContext ctx = null; UserPrincipalExt usr = null; try { log.Debug("Attempting to create new user"); if (string.IsNullOrEmpty(usersOU)) { throw new MissingFieldException("User", "usersOU"); } if (string.IsNullOrEmpty(clearTextPassword)) { throw new MissingFieldException("User", "clearTextPassword"); } if (string.IsNullOrEmpty(userObject.sAMAccountName)) { throw new MissingFieldException("User", "SamAccountName"); } if (string.IsNullOrEmpty(userObject.UserPrincipalName)) { throw new MissingFieldException("User", "UserPrincipalName"); } if (string.IsNullOrEmpty(userObject.Firstname)) { throw new MissingFieldException("User", "FirstName"); } if (string.IsNullOrEmpty(userObject.DisplayName)) { throw new MissingFieldException("User", "DisplayName"); } if (string.IsNullOrEmpty(userObject.Name)) { throw new MissingFieldException("User", "Name"); } // Check if the user exists pc = GetPrincipalContext(); // Used for querying purposes usr = UserPrincipalExt.FindByIdentity(pc, IdentityType.UserPrincipalName, userObject.UserPrincipalName); if (usr != null) { throw new PrincipalExistsException(userObject.UserPrincipalName); } // Now we can create the user! userObject.sAMAccountName = GetAvailableSamAccountName(userObject.UserPrincipalName); ctx = new PrincipalContext(ContextType.Domain, this._domainController, usersOU, this._username, this._password); // Used for creating new user usr = new UserPrincipalExt(ctx, userObject.sAMAccountName, clearTextPassword, true); usr.UserPrincipalName = userObject.UserPrincipalName; usr.DisplayName = userObject.DisplayName; usr.Name = userObject.Name; usr.GivenName = userObject.Firstname; if (!string.IsNullOrEmpty(userObject.Lastname)) { usr.LastName = userObject.Lastname; } if (!string.IsNullOrEmpty(userObject.Department)) { usr.Department = userObject.Department; } usr.Save(); // After we save we need to return some data userObject.UserGuid = (Guid)usr.Guid; userObject.DistinguishedName = usr.DistinguishedName; return(userObject); } catch (Exception ex) { log.ErrorFormat("Error retrieving user {0}. Exception: {1}", userObject.UserPrincipalName, ex.ToString()); throw; } finally { if (usr != null) { usr.Dispose(); } } }