CreateUser() public method

Adds a new membership user to the data source.
public CreateUser ( string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, MembershipCreateStatus &status ) : System.Web.Security.MembershipUser
username string The user name for the new user.
password string The password for the new user.
email string The e-mail address for the new user.
passwordQuestion string The password question for the new user.
passwordAnswer string The password answer for the new user
isApproved bool Whether or not the new user is approved to be validated.
providerUserKey object The unique identifier from the membership data source for the user.
status MembershipCreateStatus A enumeration value indicating whether the user was created successfully.
return System.Web.Security.MembershipUser
Exemplo n.º 1
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"));
        }
Exemplo n.º 2
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!"));
        }
Exemplo n.º 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.IsNotNull(user);

            string pw = provider.GetPassword("foo", null);
            Assert.AreEqual("barbar!", pw);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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"));
            }
        }
Exemplo n.º 6
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");
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 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.IsFalse(r2.IsUserInRole("foo", "Administrator"));
        }
    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);
    }
    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);
    }
    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)
      {
      }
    }
Exemplo n.º 12
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);
    }