Пример #1
0
        public void AddUserToRole()
        {
            roleProvider = new MySQLRoleProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            roleProvider.Initialize(null, config);

            AddUser("eve", "eveeve!");
            roleProvider.CreateRole("Administrator");

            roleProvider.AddUsersToRoles(new string[] { "eve" },
                                         new string[] { "Administrator" });
            Assert.True(roleProvider.IsUserInRole("eve", "Administrator"));

            roleProvider.RemoveUsersFromRoles(new string[] { "eve" }, new string[] { "Administrator" });
            Assert.False(roleProvider.IsUserInRole("eve", "Administrator"));

            roleProvider.DeleteRole("Administrator", false);
            Assert.Equal(0, roleProvider.GetAllRoles().Length);

            //clean up
            membershipProvider.DeleteUser("eve", true);
        }
Пример #2
0
        public void CrossAppLogin()
        {
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("passwordStrengthRegularExpression", "bar.*");
            config.Add("passwordFormat", "Clear");
            provider.Initialize(null, config);
            MembershipCreateStatus status;

            provider.CreateUser("foo", "bar!bar", null, null, null, true, null, out status);

            MySQLMembershipProvider provider2 = new MySQLMembershipProvider();
            NameValueCollection     config2   = new NameValueCollection();

            config2.Add("connectionStringName", "LocalMySqlServer");
            config2.Add("applicationName", "/myapp");
            config2.Add("passwordStrengthRegularExpression", ".*");
            config2.Add("passwordFormat", "Clear");
            provider2.Initialize(null, config2);

            bool worked = provider2.ValidateUser("foo", "bar!bar");

            Assert.Equal(false, worked);

            //Cleanup
            provider.DeleteUser("foo", true);
        }
Пример #3
0
        public void GetEncryptedPassword()
        {
            MembershipCreateStatus status;

            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("requiresQuestionAndAnswer", "false");
            config.Add("enablePasswordRetrieval", "true");
            config.Add("passwordFormat", "encrypted");
            config.Add("applicationName", "/");
            provider.Initialize(null, config);

            MembershipUser user = provider.CreateUser("foo", "barbar!", "*****@*****.**", null, null, true, null, out status);

            Assert.NotNull(user);

            string pw = provider.GetPassword("foo", null);

            Assert.Equal("barbar!", pw);

            //Cleanup
            provider.DeleteUser("foo", true);
        }
Пример #4
0
        public void GetPasswordWithWrongAnswer()
        {
            MembershipCreateStatus status;

            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("requiresQuestionAndAnswer", "true");
            config.Add("enablePasswordRetrieval", "true");
            config.Add("passwordFormat", "Encrypted");
            config.Add("applicationName", "/");
            provider.Initialize(null, config);
            provider.CreateUser("foo", "barbar!", "*****@*****.**", "color", "blue", true, null, out status);

            MySQLMembershipProvider provider2 = new MySQLMembershipProvider();
            NameValueCollection     config2   = new NameValueCollection();

            config2.Add("connectionStringName", "LocalMySqlServer");
            config2.Add("requiresQuestionAndAnswer", "true");
            config2.Add("enablePasswordRetrieval", "true");
            config2.Add("passwordFormat", "Encrypted");
            config2.Add("applicationName", "/");
            provider2.Initialize(null, config2);
            Assert.Throws <MembershipPasswordException>(() => provider2.GetPassword("foo", "wrong"));

            //Cleanup
            provider.DeleteUser("foo", true);
        }
        public void UserDatesNotInUtc()
        {
            var testProv = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("dateTimeUseUtc", "False");
            testProv.Initialize(null, config);

            var userCreation = DateTime.Now;
            // create the user
            MembershipCreateStatus status;
            var user = testProv.CreateUser("nab", "barbar!", "*****@*****.**", null, null, true, null, out status);

            Assert.Equal(MembershipCreateStatus.Success, status);

            Assert.Equal(Truncate(userCreation, TimeSpan.TicksPerSecond), Truncate(user.CreationDate, TimeSpan.TicksPerSecond));
            Assert.Equal(Truncate(userCreation, TimeSpan.TicksPerSecond), Truncate(user.LastLoginDate, TimeSpan.TicksPerSecond));

            Thread.Sleep(30000);

            var lastLogin = DateTime.Now;
            var validated = testProv.ValidateUser("nab", "barbar!");

            Assert.Equal(true, validated);
            user = testProv.GetUser("nab", false);

            Assert.Equal(Truncate(lastLogin, TimeSpan.TicksPerSecond), Truncate(user.LastLoginDate, TimeSpan.TicksPerSecond));

            testProv.DeleteUser("nab", true);
        }
Пример #6
0
        public void AddUserToRole()
        {
            AddUser("eve", "eveeve!");
            roleProvider.CreateRole("Administrator");

            roleProvider.AddUsersToRoles(new string[] { "eve" },
                                         new string[] { "Administrator" });
            Assert.True(roleProvider.IsUserInRole("eve", "Administrator"));

            roleProvider.RemoveUsersFromRoles(new string[] { "eve" }, new string[] { "Administrator" });
            Assert.False(roleProvider.IsUserInRole("eve", "Administrator"));

            roleProvider.DeleteRole("Administrator", false);
            Assert.AreEqual(0, roleProvider.GetAllRoles().Length);

            //clean up
            membershipProvider.DeleteUser("eve", true);
        }
Пример #7
0
        private void GetPasswordHelper(bool requireQA, bool enablePasswordRetrieval, string answer)
        {
            MembershipCreateStatus status;

            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("requiresQuestionAndAnswer", requireQA ? "true" : "false");
            config.Add("enablePasswordRetrieval", enablePasswordRetrieval ? "true" : "false");
            config.Add("passwordFormat", "clear");
            config.Add("applicationName", "/");
            config.Add("writeExceptionsToEventLog", "false");
            provider.Initialize(null, config);

            provider.CreateUser("foo", "barbar!", "*****@*****.**", "color", "blue", true, null, out status);

            string password = string.Empty;

            if (!enablePasswordRetrieval)
            {
                if (requireQA && answer != null)
                {
                    Exception ex = Assert.Throws <MembershipPasswordException>(() => provider.GetPassword("foo", answer));
                }
                else
                {
                    Exception ex = Assert.Throws <ProviderException>(() => provider.GetPassword("foo", answer));
                    Assert.Equal(ex.Message, "Password Retrieval Not Enabled.");
                }
            }
            else
            {
                if (requireQA && answer != null)
                {
                    provider.GetPassword("foo", answer);
                }
                else if (requireQA && answer == null)
                {
                    //Incorrect password answer.
                    Assert.Throws <MembershipPasswordException>(() => provider.GetPassword("foo", answer));
                }
                else
                {
                    password = provider.GetPassword("foo", answer);
                    Assert.Equal("barbar!", password);
                }
            }

            //Cleanup
            provider.DeleteUser("foo", true);
        }
Пример #8
0
        public void IsUserInRoleCrossDomain()
        {
            MySQLMembershipProvider provider = new MySQLMembershipProvider();
            NameValueCollection     config1  = new NameValueCollection();

            config1.Add("connectionStringName", "LocalMySqlServer");
            config1.Add("applicationName", "/");
            config1.Add("passwordStrengthRegularExpression", "bar.*");
            config1.Add("passwordFormat", "Clear");
            provider.Initialize(null, config1);
            MembershipCreateStatus status;

            provider.CreateUser("foo", "bar!bar", null, null, null, true, null, out status);

            MySQLMembershipProvider provider2 = new MySQLMembershipProvider();
            NameValueCollection     config2   = new NameValueCollection();

            config2.Add("connectionStringName", "LocalMySqlServer");
            config2.Add("applicationName", "/myapp");
            config2.Add("passwordStrengthRegularExpression", ".*");
            config2.Add("passwordFormat", "Clear");
            provider2.Initialize(null, config2);

            roleProvider = new MySQLRoleProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            roleProvider.Initialize(null, config);

            MySQLRoleProvider   r2       = new MySQLRoleProvider();
            NameValueCollection configr2 = new NameValueCollection();

            configr2.Add("connectionStringName", "LocalMySqlServer");
            configr2.Add("applicationName", "/myapp");
            r2.Initialize(null, configr2);

            roleProvider.CreateRole("Administrator");
            roleProvider.AddUsersToRoles(new string[] { "foo" },
                                         new string[] { "Administrator" });
            Assert.False(r2.IsUserInRole("foo", "Administrator"));

            roleProvider.DeleteRole("Administrator", false);
            Assert.AreEqual(0, roleProvider.GetAllRoles().Length);

            //Cleanup
            provider.DeleteUser("foo", true);
        }
Пример #9
0
        public void CreateUserWithErrors()
        {
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("passwordStrengthRegularExpression", "bar.*");
            config.Add("passwordFormat", "Hashed");
            provider.Initialize(null, config);

            // first try to create a user with a password not long enough
            MembershipCreateStatus status;
            MembershipUser         user = provider.CreateUser("foo", "xyz",
                                                              "*****@*****.**", null, null, true, null, out status);

            Assert.Null(user);
            Assert.Equal(MembershipCreateStatus.InvalidPassword, status);

            // now with not enough non-alphas
            user = provider.CreateUser("foo", "xyz1234",
                                       "*****@*****.**", null, null, true, null, out status);
            Assert.Null(user);
            Assert.Equal(MembershipCreateStatus.InvalidPassword, status);

            // now one that doesn't pass the regex test
            user = provider.CreateUser("foo", "xyzxyz!",
                                       "*****@*****.**", null, null, true, null, out status);
            Assert.Null(user);
            Assert.Equal(MembershipCreateStatus.InvalidPassword, status);

            // now one that works
            user = provider.CreateUser("foo", "barbar!",
                                       "*****@*****.**", null, null, true, null, out status);
            Assert.NotNull(user);
            Assert.Equal(MembershipCreateStatus.Success, status);

            //Cleanup
            provider.DeleteUser("foo", true);
        }
Пример #10
0
        public void ResetPassword()
        {
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("passwordStrengthRegularExpression", "bar.*");
            config.Add("passwordFormat", "Clear");
            config.Add("requiresQuestionAndAnswer", "false");
            provider.Initialize(null, config);

            MembershipCreateStatus status;

            provider.CreateUser("foo", "bar!bar", null, null, null, true, null, out status);

            MembershipUser u     = provider.GetUser("foo", false);
            string         newpw = provider.ResetPassword("foo", null);

            //Cleanup
            provider.DeleteUser("foo", true);
        }
Пример #11
0
        public void DeleteUser()
        {
            CreateUserWithHashedPassword();
            Assert.IsTrue(provider.DeleteUser("foo", true));
            DataTable table = FillTable("SELECT * FROM my_aspnet_Membership");

            Assert.AreEqual(0, table.Rows.Count);
            table = FillTable("SELECT * FROM my_aspnet_Users");
            Assert.AreEqual(0, table.Rows.Count);

            CreateUserWithHashedPassword();
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();

            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            provider.Initialize(null, config);
            Assert.IsTrue(Membership.DeleteUser("foo", false));
            table = FillTable("SELECT * FROM my_aspnet_Membership");
            Assert.AreEqual(0, table.Rows.Count);
            table = FillTable("SELECT * FROM my_aspnet_Users");
            Assert.AreEqual(1, table.Rows.Count);
        }