Manages storage of membership information for an ASP.NET application in a MySQL database.

This class is used by the Membership and MembershipUser classes to provide membership services for ASP.NET applications using a MySQL database.

Наследование: System.Web.Security.MembershipProvider
Пример #1
0
        protected void LoadSchema(int version)
        {
            if (version < 1) return;

              MySQLMembershipProvider provider = new MySQLMembershipProvider();

              ResourceManager r = new ResourceManager("MySql.Web.Properties.Resources", typeof(MySQLMembershipProvider).Assembly);
              string schema = r.GetString(String.Format("schema{0}", version));
              MySqlScript script = new MySqlScript(conn);
              script.Query = schema;

              try
              {
            script.Execute();
              }
              catch (MySqlException ex)
              {
            if (ex.Number == 1050 && version == 7)
            {
              // Schema7 performs several renames of tables to their lowercase representation.
              // If the current server OS does not support renaming to lowercase, then let's just continue.
              script.Query = "UPDATE my_aspnet_schemaversion SET version=7";
              script.Execute();
            }
              }
        }
Пример #2
0
        public void ChangeAppName()
        {
            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);
              Assert.IsTrue(status == MembershipCreateStatus.Success);

              MySQLMembershipProvider provider2 = new MySQLMembershipProvider();
              NameValueCollection config2 = new NameValueCollection();
              config2.Add("connectionStringName", "LocalMySqlServer");
              config2.Add("applicationName", "/myapp");
              config2.Add("passwordStrengthRegularExpression", "foo.*");
              config2.Add("passwordFormat", "Clear");
              provider2.Initialize(null, config2);
              provider2.CreateUser("foo2", "foo!foo", null, null, null, true, null, out status);
              Assert.IsTrue(status == MembershipCreateStatus.Success);

              provider.ApplicationName = "/myapp";
              Assert.IsFalse(provider.ValidateUser("foo", "bar!bar"));
              Assert.IsTrue(provider.ValidateUser("foo2", "foo!foo"));
        }
Пример #3
0
    public void SchemaCheck()
    {
      for (int i = 0; i <= SchemaManager.Version; i++)
      {
        DropAllTables();
        MySQLMembershipProvider provider = new MySQLMembershipProvider();
        NameValueCollection config = new NameValueCollection();
        config.Add("connectionStringName", "LocalMySqlServer");
        config.Add("applicationName", "/");
        config.Add("passwordFormat", "Clear");

        if (i > 0)
          for (int x = 1; x <= i; x++)
            LoadSchema(x);

        try
        {
          provider.Initialize(null, config);
          if (i < SchemaManager.Version)
            Assert.Fail("Should have failed");
        }
        catch (ProviderException)
        {
          if (i == SchemaManager.Version)
            Assert.Fail("This should not have failed");
        }
      }
    }
Пример #4
0
		public override void Setup()
		{
			base.Setup();

			execSQL("DROP TABLE IF EXISTS mysql_membership");
			execSQL("DROP TABLE IF EXISTS mysql_roles");

			membershipProvider = new MySQLMembershipProvider();
			NameValueCollection config = new NameValueCollection();
			config.Add("connectionStringName", "LocalMySqlServer");
			config.Add("applicationName", "/");
			membershipProvider.Initialize(null, config);
		}
Пример #5
0
        public void AutoGenerateSchema()
        {
            MySQLMembershipProvider provider = new MySQLMembershipProvider();
              NameValueCollection config = new NameValueCollection();
              config.Add("connectionStringName", "LocalMySqlServer");
              config.Add("autogenerateschema", "true");
              config.Add("applicationName", "/");
              config.Add("passwordFormat", "Clear");

              provider.Initialize(null, config);

              MembershipCreateStatus status;
              MembershipUser user = provider.CreateUser("boo", "password", "*****@*****.**",
              "question", "answer", true, null, out status);
        }
Пример #6
0
        public void CurrentSchema()
        {
            MySQLMembershipProvider provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("connectionStringName", "LocalMySqlServer");
            provider.Initialize(null, config);

            MySqlCommand cmd = new MySqlCommand("SHOW CREATE TABLE mysql_membership", conn);
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                string createTable = reader.GetString(1);
                int index = createTable.IndexOf("COMMENT='2'");
                Assert.AreNotEqual(-1, index);
            }
        }
Пример #7
0
        public void SchemaNotPresent()
        {
            MySQLMembershipProvider provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("passwordFormat", "Clear");

            try
            {
                provider.Initialize(null, config);
                Assert.Fail("Should have failed");
            }
            catch (ProviderException)
            {
            }
        }
Пример #8
0
        private void CreateUserWithFormat(MembershipPasswordFormat format)
        {
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("passwordStrengthRegularExpression", "bar.*");
            config.Add("passwordFormat", format.ToString());
            provider.Initialize(null, config);

            // create the user
            MembershipCreateStatus status;
            provider.CreateUser("foo", "barbar!", "*****@*****.**", null, null, true, null, out status);
            Assert.AreEqual(MembershipCreateStatus.Success, status);

            // verify that the password format is hashed.
            DataTable table = FillTable("SELECT * FROM my_aspnet_Membership");
            MembershipPasswordFormat rowFormat =
                (MembershipPasswordFormat)Convert.ToInt32(table.Rows[0]["PasswordFormat"]);
            Assert.AreEqual(format, rowFormat);

            //  then attempt to verify the user
            Assert.IsTrue(provider.ValidateUser("foo", "barbar!"));
        }
Пример #9
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", "/");
            provider.Initialize(null, config);

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

            try
            {
                string password = provider.GetPassword("foo", answer);
                if (!enablePasswordRetrieval)
                    Assert.Fail("This should have thrown an exception");
                Assert.AreEqual("barbar!", password);
            }
            catch (ProviderException)
            {
                if (requireQA && answer != null)
                    Assert.Fail("This should not have thrown an exception");
            }
        }
Пример #10
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);
        }
Пример #11
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.IsNull(user);
            Assert.AreEqual(MembershipCreateStatus.InvalidPassword, status);

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

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

            // now one that works
            user = provider.CreateUser("foo", "barbar!",
                "*****@*****.**", null, null, true, null, out status);
            Assert.IsNotNull(user);
            Assert.AreEqual(MembershipCreateStatus.Success, status);
        }
Пример #12
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.IsFalse(r2.IsUserInRole("foo", "Administrator"));
        }
Пример #13
0
        public void MinRequiredAlpha()
        {
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("applicationName", "/");
            config.Add("minRequiredNonalphanumericCharacters", "3");
            provider.Initialize(null, config);

            MembershipCreateStatus status;
            MembershipUser user = provider.CreateUser("foo", "pw!pass", "email", null, null, true, null, out status);
            Assert.IsNull(user);

            user = provider.CreateUser("foo", "pw!pa!!", "email", null, null, true, null, out status);
            Assert.IsNotNull(user);
        }
    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);

      try
      {
        string pw = provider2.GetPassword("foo", "wrong");
        Assert.Fail("Should have  failed");
      }
      catch (MembershipPasswordException)
      {
      }
    }
Пример #15
0
    public void InitializeInvalidConnStringThrowsArgumentException()
    {
      Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      string connStr = configFile.ConnectionStrings.ConnectionStrings["LocalMySqlServer"].ConnectionString;
      string fakeConnectionString = connStr.Replace("database", "fooKey");
      try
      {
        configFile.ConnectionStrings.ConnectionStrings["LocalMySqlServer"].ConnectionString = fakeConnectionString;
        configFile.Save();
        ConfigurationManager.RefreshSection("connectionStrings");

        MySQLMembershipProvider provider = new MySQLMembershipProvider();
        NameValueCollection config = new NameValueCollection();
        config.Add("connectionStringName", "LocalMySqlServer");

        provider.Initialize(null, config);
      }
      finally
      {
        configFile.ConnectionStrings.ConnectionStrings["LocalMySqlServer"].ConnectionString = connStr;
        configFile.Save();
        ConfigurationManager.RefreshSection("connectionStrings");
      }
    }
Пример #16
0
        protected void LoadSchema(int version)
        {
            if (version < 1) return;

            MySQLMembershipProvider provider = new MySQLMembershipProvider();

            ResourceManager r = new ResourceManager("MySql.Web.Properties.Resources", typeof(MySQLMembershipProvider).Assembly);
            string schema = r.GetString(String.Format("schema{0}", version));
            MySqlScript script = new MySqlScript(conn);
            script.Query = schema;
            script.Execute();
        }
Пример #17
0
        public void CreateUserWithNoQA()
        {
            MembershipCreateStatus status;
            provider = new MySQLMembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("connectionStringName", "LocalMySqlServer");
            config.Add("requiresQuestionAndAnswer", "true");
            config.Add("passwordFormat", "clear");
            config.Add("applicationName", "/");
            provider.Initialize(null, config);

            try
            {
                provider.CreateUser("foo", "barbar!", "*****@*****.**", "color", null, true, null, out status);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.StartsWith("Password answer supplied is invalid"));
            }
            try
            {
                provider.CreateUser("foo", "barbar!", "*****@*****.**", "", "blue", true, null, out status);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.StartsWith("Password question supplied is invalid"));
            }
        }
    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.AreEqual(false, worked);
    }
Пример #19
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.IsNotNull(user);

            string pw = provider.GetPassword("foo", null);
            Assert.AreEqual("barbar!", pw);
        }
    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);
    }