public void testCompleteInitLibrary()
        {

            LdapUser adminUser = new LdapUser(ConfigurationManager.AppSettings["LDAPAdminUserDN"],
                                                ConfigurationManager.AppSettings["LDAPAdminUserCN"],
                                                ConfigurationManager.AppSettings["LDAPAdminUserSN"],
                                                null);
            adminUser.CreateUserAttribute("userPassword", ConfigurationManager.AppSettings["LDAPAdminUserPassword"]);

            AuthType authType = (AuthType)Enum.Parse(typeof(AuthType),
                                                        ConfigurationManager.AppSettings["LDAPAuthType"]);

            LdapManagerObj = new LdapManager(adminUser,
                                                ConfigurationManager.AppSettings["LDAPServer"],
                                                ConfigurationManager.AppSettings["LDAPSearchBaseDN"],
                                                authType,
                                                Convert.ToBoolean(ConfigurationManager.AppSettings["secureSocketLayerFlag"]),
                                                Convert.ToBoolean(ConfigurationManager.AppSettings["transportSocketLayerFlag"]),
                                                Convert.ToBoolean(ConfigurationManager.AppSettings["ClientCertificationFlag"]),
                                                ConfigurationManager.AppSettings["clientCertificatePath"],
                                                Convert.ToBoolean(ConfigurationManager.AppSettings["enableLDAPLibraryLog"]),
                                                ConfigurationManager.AppSettings["LDAPLibraryLogPath"],
                                                ConfigurationManager.AppSettings["LdapUserObjectClass"],
                                                ConfigurationManager.AppSettings["LDAPMatchFieldUsername"]
                                                );

            Assert.IsFalse(LdapManagerObj.Equals(null));
        }
        public static ILdapManager SetupLdapLibrary()
        {
            var authType = (AuthType) Enum.Parse(typeof (AuthType),
                Config.LdapLibrary["LDAPAuthType"]);

            if (string.IsNullOrEmpty(Config.LdapLibrary["LDAPAdminUserDN"]))
                return new LdapManager(null,
                    (LDAPAdminMode)Enum.Parse(typeof(LDAPAdminMode), Config.LdapLibrary["LDAPAdminMode"]),
                    Config.LdapLibrary["LDAPServer"],
                    Config.LdapLibrary["LDAPSearchBaseDN"],
                    authType,
                    (LoggerType) Enum.Parse(typeof (LoggerType), Config.LdapLibrary["enableLDAPLibraryLog"]),
                    Config.LdapLibrary["LDAPLibraryLogPath"]
                    );

            var adminUser = new LdapUser(Config.LdapLibrary["LDAPAdminUserDN"],
                Config.LdapLibrary["LDAPAdminUserCN"],
                Config.LdapLibrary["LDAPAdminUserSN"],
                null);

            adminUser.CreateUserAttribute("userPassword", Config.LdapLibrary["LDAPAdminUserPassword"]);

            return new LdapManager(adminUser,
                (LDAPAdminMode)Enum.Parse(typeof(LDAPAdminMode), Config.LdapLibrary["LDAPAdminMode"]),
                Config.LdapLibrary["LDAPServer"],
                Config.LdapLibrary["LDAPSearchBaseDN"],
                authType,
                (LoggerType) Enum.Parse(typeof (LoggerType), Config.LdapLibrary["enableLDAPLibraryLog"]),
                Config.LdapLibrary["LDAPLibraryLogPath"]
                );
        }
        public void LdapUserInstance()
        {
            var user = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                LdapUserAttributes);

            Assert.IsNotNull(user);
        }
        public bool TestCompleteInitLibrary()
        {
            try
            {
                var adminUser = new LdapUser(Config.LdapLibrary["LDAPAdminUserDN"],
                    Config.LdapLibrary["LDAPAdminUserCN"],
                    Config.LdapLibrary["LDAPAdminUserSN"],
                    null);
                adminUser.CreateUserAttribute("userPassword", Config.LdapLibrary["LDAPAdminUserPassword"]);

                var authType = (AuthType) Enum.Parse(typeof (AuthType),
                    Config.LdapLibrary["LDAPAuthType"]);

                _ldapManagerObj = new LdapManager(adminUser,
                    (LDAPAdminMode) Enum.Parse(typeof (LDAPAdminMode),Config.LdapLibrary["LDAPAdminMode"]),
                    Config.LdapLibrary["LDAPServer"],
                    Config.LdapLibrary["LDAPSearchBaseDN"],
                    authType,
                    Convert.ToBoolean(Config.LdapLibrary["secureSocketLayerFlag"]),
                    Convert.ToBoolean(Config.LdapLibrary["transportSocketLayerFlag"]),
                    Convert.ToBoolean(Config.LdapLibrary["ClientCertificationFlag"]),
                    Config.LdapLibrary["clientCertificatePath"],
                    (LoggerType) Enum.Parse(typeof (LoggerType), Config.LdapLibrary["enableLDAPLibraryLog"]),
                    Config.LdapLibrary["LDAPLibraryLogPath"],
                    Config.LdapLibrary["LDAPUserObjectClass"],
                    Config.LdapLibrary["LDAPMatchFieldUsername"]
                    );

                if (!_ldapManagerObj.Equals(null))
                    return true;
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
 private void ResetTestUser()
 {
     _testUser = new LdapUser(LdapUserDn,
     LdapUserCn,
     LdapUserSn,
     LdapUserAttributes);
 }
        public void DeleteUserAttribute()
        {
            var user = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                new Dictionary<string, List<string>>
                {
                    {"description", new List<string> {"test description"}},
                });

            const string descriptionToDelete = "test description";

            user.DeleteUserAttribute("description", descriptionToDelete);

            Console.WriteLine(user.GetUserAttribute("description"));
            Assert.IsTrue(!user.GetUserAttribute("description").Contains(descriptionToDelete));
            Assert.IsTrue(user.GetUserAttribute("description").Count == 0);
        }
        public void InsertUserAttribute()
        {
            var user = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                new Dictionary<string, List<string>>
                {
                    {"description", new List<string> {"test description"}}
                });
            const string addictionalDescription = "new test description Inserted";

            user.InsertUserAttribute("description", addictionalDescription);

            Assert.IsTrue(user.GetUserAttribute("description").Contains(addictionalDescription));

            user.InsertUserAttribute("description", null);
            user.InsertUserAttribute("description", "");

            Assert.IsTrue(user.GetUserAttribute("description").Count == 2);
        }
        public void TestCreateUser()
        {
            var tempUser = new LdapUser(WriteUserDn, WriteUserCn, "test", null);
            var existingUser = new LdapUser(ReadOnlyUserDn, ReadOnlyUserCn, "test",
                new Dictionary<string, List<string>> {{"userPassword", new List<string> {ReadOnlyUserPwd}}});

            //Init the DLL and connect the admin
            TestAdminConnect();

            //Create existing user
            var result = _ldapManagerObj.CreateUser(existingUser);

            //Assert the correct operations
            Assert.IsFalse(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1), "LDAP CREATE USER ERROR: ");

            //Create user
            result = _ldapManagerObj.CreateUser(tempUser);

            //Assert the correct operations
            Assert.IsTrue(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1),
                "LDAP USER MANIPULATION SUCCESS: ");

            result = _ldapManagerObj.DeleteUser(tempUser);

            Assert.IsTrue(result);
        }
        public void CreateUserAttributeList()
        {
            var user = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                LdapUserAttributes);
            var newAttribute = new List<string> {"new test value 1", "new test value 2"};

            user.CreateUserAttribute("newAttribute", newAttribute);

            CollectionAssert.AreEqual(user.GetUserAttribute("newAttribute"), newAttribute);
        }
        public void OverwriteUserAttributeSingle()
        {
            var testUserCopy = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                LdapUserAttributes);
            const string descriptions = "new test description 1";
            const string telephoneNumbers = "123456789";

            testUserCopy.OverwriteUserAttribute("description", descriptions);
            testUserCopy.OverwriteUserAttribute("telephoneNumber", telephoneNumbers);

            CollectionAssert.AreEqual(testUserCopy.GetUserAttribute("description"), new List<string> {descriptions});
            CollectionAssert.AreEqual(testUserCopy.GetUserAttribute("telephoneNumber"),
                new List<string> {telephoneNumbers});
        }
        public void OverwriteUserAttributeList()
        {
            var testUserCopy = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                LdapUserAttributes);
            var descriptions = new List<string> {"new test description 1", "new test description 2"};
            var telephoneNumbers = new List<string> {"123456789", "987654321"};

            testUserCopy.OverwriteUserAttribute("description", descriptions);
            testUserCopy.OverwriteUserAttribute("telephoneNumber", telephoneNumbers);

            CollectionAssert.AreEqual(testUserCopy.GetUserAttribute("description"), descriptions);
            CollectionAssert.AreEqual(testUserCopy.GetUserAttribute("telephoneNumber"), telephoneNumbers);
        }
        public void TestSearchUserAndConnect()
        {
            TestAdminConnect();
            var testLdapUser = new LdapUser(WriteUserDn, WriteUserCn, "test",
                new Dictionary<string, List<string>> {{"userPassword", new List<string> {WriteUserPwd}}});

            var result = _ldapManagerObj.CreateUser(testLdapUser);

            Assert.IsTrue(result);

            result = _ldapManagerObj.SearchUserAndConnect(WriteUserCn, WriteUserPwd);

            Assert.IsTrue(result);

            TestAdminConnect();

            result = _ldapManagerObj.DeleteUser(testLdapUser);

            Assert.IsTrue(result);
        }
        public void TestChangeUserPassword()
        {
            TestAdminConnect();
            const string newPassword = "******";
            var testUser = new LdapUser(WriteUserDn, WriteUserCn, "test",
                new Dictionary<string, List<string>> {{"userPassword", new List<string> {WriteUserPwd}}});
            //Create the user
            var result = _ldapManagerObj.CreateUser(testUser);

            Assert.IsTrue(result);

            //Perform change of password
            result = _ldapManagerObj.ChangeUserPassword(testUser, newPassword);
            Assert.IsTrue(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1),
                "LDAP USER MANIPULATION SUCCESS: ");

            //Try to connect with the old password
            var testUserCredential = new NetworkCredential(
                testUser.GetUserDn(),
                WriteUserPwd,
                "");

            result = _ldapManagerObj.Connect(testUserCredential);

            Assert.IsFalse(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1), "LDAP CONNECTION ERROR: ");

            //Try to connect with the new password
            testUserCredential = new NetworkCredential(
                testUser.GetUserDn(),
                newPassword,
                "");

            result = _ldapManagerObj.Connect(testUserCredential);

            Assert.IsTrue(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1), "LDAP CONNECTION SUCCESS");

            TestAdminConnect();

            result = _ldapManagerObj.DeleteUser(testUser);

            Assert.IsTrue(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1),
                "LDAP USER MANIPULATION SUCCESS: ");
        }
        public void TestModifyUserAttribute()
        {
            TestAdminConnect();
            var testLdapUser = new LdapUser(WriteUserDn, WriteUserCn, "test",
                new Dictionary<string, List<string>> {{"description", new List<string> {"test"}}});
            var result = _ldapManagerObj.CreateUser(testLdapUser);

            Assert.IsTrue(result);

            List<ILdapUser> returnUsers;
            const string userAttributeValue = "description Modified";

            result = _ldapManagerObj.ModifyUserAttribute(DirectoryAttributeOperation.Delete, testLdapUser, "ciccio",
                userAttributeValue);

            Assert.IsFalse(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1),
                "LDAP MODIFY USER ATTRIBUTE ERROR: ");

            result = _ldapManagerObj.ModifyUserAttribute(DirectoryAttributeOperation.Replace, testLdapUser,
                "description", userAttributeValue);

            Assert.IsTrue(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1),
                "LDAP USER MANIPULATION SUCCESS: ");

            result = _ldapManagerObj.SearchUsers(
                new List<string> {"description"},
                new[] {WriteUserCn},
                out returnUsers);

            Assert.IsTrue(result);
            Assert.AreEqual(returnUsers[0].GetUserCn(), testLdapUser.GetUserCn());
            Assert.AreEqual(returnUsers[0].GetUserAttribute("description")[0], userAttributeValue);

            result = _ldapManagerObj.DeleteUser(testLdapUser);

            Assert.IsTrue(result);
        }
        public void TestDeleteUser()
        {
            //Set the test user
            var testLdapUser = new LdapUser(WriteUserDn, WriteUserCn, "test", null);

            //Init the DLL and connect the admin
            TestAdminConnect();

            //Create LDAPUser to delete.
            var result = _ldapManagerObj.CreateUser(testLdapUser);

            Assert.IsTrue(result);

            //Delete user
            result = _ldapManagerObj.DeleteUser(testLdapUser);

            //Assert the correct operations
            Assert.IsTrue(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1),
                "LDAP USER MANIPULATION SUCCESS: ");

            //Delete user again with error
            result = _ldapManagerObj.DeleteUser(testLdapUser);

            //Assert the correct operations
            Assert.IsFalse(result);
            Assert.AreEqual(_ldapManagerObj.GetLdapMessage().Split('-')[1].Substring(1), "LDAP DELETE USER ERROR: ");
        }
        private LdapUser setupReadOnlyTestUser()
        {
            string userDN = "cn=Matteo,o=ApexNet,ou=People,dc=maxcrc,dc=com";
            string userCN = "Matteo";
            string userSN = "Paci";

            LdapUser testLdapUser = new LdapUser(userDN, userCN, userSN, null);

            testLdapUser.CreateUserAttribute("userPassword", "1");
            testLdapUser.CreateUserAttribute("description", "test");

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["LDAPMatchFieldUsername"]))
            {
                if (ConfigurationManager.AppSettings["LDAPMatchFieldUsername"].Equals("cn"))
                    LDAPMatchSearchField = new string[1] { testLdapUser.GetUserCn() };
                else if (ConfigurationManager.AppSettings["LDAPMatchFieldUsername"].Equals("dn"))
                    LDAPMatchSearchField = new string[1] { testLdapUser.GetUserDn() };
                else if (ConfigurationManager.AppSettings["LDAPMatchFieldUsername"].Equals("sn"))
                    LDAPMatchSearchField = new string[1] { testLdapUser.GetUserSn() };
                else
                    LDAPMatchSearchField = new string[1] {
						testLdapUser.GetUserAttribute( ConfigurationManager.AppSettings["LDAPMatchFieldUsername"] )[0]
					};
            }

            //Set the test user
            return testLdapUser;
        }
        public void testStandardInitLibrary()
        {

            LdapUser adminUser = new LdapUser(ConfigurationManager.AppSettings["LDAPAdminUserDN"],
                                                 ConfigurationManager.AppSettings["LDAPAdminUserCN"],
                                                 ConfigurationManager.AppSettings["LDAPAdminUserSN"],
                                                 null);
            adminUser.CreateUserAttribute("userPassword", ConfigurationManager.AppSettings["LDAPAdminUserPassword"]);


            AuthType authType = (AuthType)Enum.Parse(typeof(AuthType),
                                                        ConfigurationManager.AppSettings["LDAPAuthType"]);

            LdapManagerObj = new LdapManager(adminUser,
                                                ConfigurationManager.AppSettings["LDAPServer"],
                                                ConfigurationManager.AppSettings["LDAPSearchBaseDN"],
                                                authType
                                                );

            Assert.IsFalse(LdapManagerObj.Equals(null));
        }
        public void CreateUserAttributeSingle()
        {
            var user = new LdapUser(LdapUserDn,
                LdapUserCn,
                LdapUserSn,
                LdapUserAttributes);
            const string newAttribute = "new test value 1";

            user.CreateUserAttribute("newAttribute1", newAttribute);

            CollectionAssert.AreEqual(user.GetUserAttribute("newAttribute1"), new List<string> {newAttribute});
        }