/* * Edit funcations */ private string CreateAdUser(AdUser adUser, string container, List<string> securityGroups) { string loginId, manager = string.Empty; // find the supervisor if (!string.IsNullOrEmpty(adUser.ManagerKerb)) { var supervisor = GetUserByEmployeeId(adUser.ManagerKerb); if (supervisor != null) manager = supervisor.DistinguishedName; } using (var upc = new PrincipalContext(ContextType.Domain, Site.ActiveDirectoryServer, container, UserName, Password)) { loginId = CheckForExistingUser(adUser.FirstName, adUser.LastName, upc); if (loginId == null) { throw new DuplicateNameException("Unable to determine a valid userid for the requested user."); } var user = new UserPrincipal(upc); AutoMapper.Mapper.Map(adUser, user); user.SamAccountName = loginId; user.UserPrincipalName = string.Format("{0}@caesdo.caes.ucdavis.edu", loginId); user.Enabled = true; if (adUser.LastName.ToLower() != loginId) { user.Name = string.Format("{0}, {1} ({2})", adUser.LastName, adUser.FirstName, loginId); } user.SetPassword(GeneratePassword(16)); //if (adUser.NeedsEmail) //{ // user.EmailAddress = string.Format("{0}@caes.ucdavis.edu", loginId); //} user.Save(); foreach (var groupId in securityGroups) { AddToGroup(user, groupId); } } // assign attributes that must be done after saving using (var ad = new PrincipalContext(ContextType.Domain, Site.ActiveDirectoryServer, container, UserName, Password)) { var user = UserPrincipal.FindByIdentity(ad, loginId); // set the extended properties that cannot be done before first save user.OfficeLocation(adUser.OfficeLocation); user.Manager(manager); user.Save(); } return loginId; }
public string CreateUser(AdUser adUser, string container, List<string> securityGroups, bool needsMailbox, string exchangeDatabase = null) { var loginId = CreateAdUser(adUser, container, securityGroups); if (needsMailbox && !string.IsNullOrEmpty(_exchangeUri) && !string.IsNullOrEmpty(exchangeDatabase)) { CreateMailbox(loginId, string.Format("{0}, {1}", adUser.LastName, adUser.FirstName), exchangeDatabase); if (!string.IsNullOrEmpty(_lyncUri)) { Thread.Sleep(5000); EnableLync(loginId); } } DisableAccount(loginId, container); return loginId; }