/// <summary> /// Updates a user in Active Directory /// </summary> /// <param name="user"></param> /// <param name="isUsingDisplayNameAsNameAttribute"></param> public void UpdateUser(UsersObject user, bool isUsingDisplayNameAsNameAttribute) { PrincipalContext pc = null; UserPrincipalExt up = null; try { pc = new PrincipalContext(ContextType.Domain, this.domainController, this.username, this.password); logger.Debug("Finding user in Active Directory: " + user.UserPrincipalName); up = UserPrincipalExt.FindByIdentity(pc, IdentityType.UserPrincipalName, user.UserPrincipalName); if (up == null) { throw new Exception("USER IS UNKNOWN"); } else { up.GivenName = user.Firstname; up.DisplayName = user.DisplayName; up.Enabled = user.IsEnabled; if (!string.IsNullOrEmpty(user.Middlename)) { up.MiddleName = user.Middlename; } else { up.MiddleName = null; } if (!string.IsNullOrEmpty(user.Lastname)) { up.LastName = user.Lastname; } else { up.LastName = null; } if (!string.IsNullOrEmpty(user.Department)) { up.Department = user.Department; } else { up.Department = null; } if (isUsingDisplayNameAsNameAttribute) { up.Name = user.DisplayName; } // Save changes up.Save(); } } catch (Exception ex) { this.logger.Error("Error updating user " + user.UserPrincipalName, ex); throw; } finally { if (up != null) { up.Dispose(); } if (pc != null) { pc.Dispose(); } } }
/// <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(); } } }