public void Save_ShouldHandleUserPrincipal() { const string userName = "******"; var principalRepository = new PrincipalRepository(CreateDefaultDomainPrincipalConnection()); using (var userPrincipalQueryFilter = new UserPrincipalQueryFilter()) { userPrincipalQueryFilter.SamAccountName = userName; using (var foundUserPrincipals = principalRepository.Find(userPrincipalQueryFilter)) { if (foundUserPrincipals.Any()) { if (foundUserPrincipals.Count() > 1) { throw new InvalidOperationException("There should not be duplicates of a user."); } var userPrincipal = (IUserPrincipal)foundUserPrincipals.ElementAt(0); principalRepository.Delete(userPrincipal); } } } using (var userPrincipal = principalRepository.Create <IUserPrincipal>()) { userPrincipal.Enabled = true; userPrincipal.PasswordNeverExpires = true; userPrincipal.SamAccountName = userName; userPrincipal.SetPassword("P@ssword"); principalRepository.Save(userPrincipal); } }