public void Properties_CreateUser_CheckAllProperties() {
			var user = A.Fake<IUser>();
			A.CallTo(() => user.Id).Returns("userid");

			A.CallTo(() => user.Properties.CreationDate).Returns(DateTime.Now);
			A.CallTo(() => user.Properties.Locked).Returns(true);
			A.CallTo(() => user.Properties.LastLockoutDate).Returns(DateTime.Now);
			A.CallTo(() => user.Properties.LastPasswordChangedDate).Returns(DateTime.Now);
			A.CallTo(() => user.Properties.PasswordQuestion).Returns("What is the meaning of life?");
			A.CallTo(() => user.Properties.LastLoginDate).Returns(DateTime.Now);
			A.CallTo(() => user.Properties.Description).Returns("Dummy user");
			A.CallTo(() => user.Properties.Email).Returns("*****@*****.**");
			A.CallTo(() => user.Properties.Active).Returns(true);
			A.CallTo(() => user.Properties.LastActivityDate).Returns(DateTime.Now);


			var memebershipUser = new MembershipUser(user, "provider");
			Assert.AreEqual("provider", memebershipUser.ProviderName);
			Assert.AreEqual(user.Properties.CreationDate, memebershipUser.CreationDate);
			Assert.AreEqual(user.Properties.Locked, memebershipUser.IsLockedOut);
			Assert.AreEqual(user.Properties.LastLockoutDate, memebershipUser.LastLockoutDate);
			Assert.AreEqual(user.Properties.LastPasswordChangedDate, memebershipUser.LastPasswordChangedDate);
			Assert.AreEqual(user.Properties.PasswordQuestion, memebershipUser.PasswordQuestion);
			Assert.AreEqual(user.Properties.LastLoginDate, memebershipUser.LastLoginDate);
			Assert.AreEqual(user.Properties.Description, memebershipUser.Comment);
			Assert.AreEqual(user.Properties.Email, memebershipUser.Email);
			Assert.AreEqual(user.Properties.Active, memebershipUser.IsApproved);
			Assert.AreEqual(user.Properties.LastActivityDate, memebershipUser.LastActivityDate);


		}
		public void UserName_CreateUserWithDomain_CreateUsernameWithDomain() {
			var user = A.Fake<IUser>();
			A.CallTo(() => user.UserName).Returns("username");
			A.CallTo(() => user.Domain).Returns("njupiter");
			var memebershipUser = new MembershipUser(user, "provider");
			Assert.AreEqual("njupiter\\username", memebershipUser.UserName);
		}
Example #3
0
        /// <summary>
        /// Adds a new membership user to the data source.
        /// </summary>
        /// <param name="username">The user name for the new user.</param>
        /// <param name="password">The password for the new user.</param>
        /// <param name="email">The e-mail address for the new user.</param>
        /// <param name="passwordQuestion">The password question for the new user.</param>
        /// <param name="passwordAnswer">The password answer for the new user</param>
        /// <param name="isApproved">Whether or not the new user is approved to be validated.</param>
        /// <param name="providerUserKey">The unique identifier from the membership data source for the user.</param>
        /// <param name="status">A <see cref="T:System.Web.Security.MembershipCreateStatus"/> enumeration value indicating whether the user was created successfully.</param>
        /// <returns>
        /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the information for the newly created user.
        /// </returns>
        public override System.Web.Security.MembershipUser CreateUser(string username,
                                                                      string password,
                                                                      string email,
                                                                      string passwordQuestion,
                                                                      string passwordAnswer,
                                                                      bool isApproved,
                                                                      object providerUserKey,
                                                                      out MembershipCreateStatus status)
        {
            if (username == null)
            {
                throw new ArgumentNullException("username");
            }
            if (!ValidateParameter(password, true, true, false, 256))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }
            if (!ValidateParameter(username, true, true, true, 256))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return(null);
            }
            if (!ValidateParameter(email, RequiresUniqueEmail, RequiresUniqueEmail, false, 256))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return(null);
            }
            if (!ValidateParameter(passwordQuestion, RequiresQuestionAndAnswer, true, false, 256))
            {
                status = MembershipCreateStatus.InvalidQuestion;
                return(null);
            }
            if (!ValidateParameter(passwordAnswer, RequiresQuestionAndAnswer, true, false, 256))
            {
                status = MembershipCreateStatus.InvalidAnswer;
                return(null);
            }
            if (password.Length < MinRequiredPasswordLength)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }
            var numberOfNonAlphaChars = 0;

            for (var i = 0; i < password.Length; i++)
            {
                if (!char.IsLetterOrDigit(password, i))
                {
                    numberOfNonAlphaChars++;
                }
            }
            if (numberOfNonAlphaChars < MinRequiredNonAlphanumericCharacters)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }
            if ((PasswordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(password, PasswordStrengthRegularExpression))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }
            var args = new ValidatePasswordEventArgs(username, password, true);

            OnValidatingPassword(args);
            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }
            try {
                var name   = GetUserNameFromMembershipUserName(username);
                var domain = GetDomainFromMembershipUserName(username);
                var user   = UserRepository.CreateUserInstance(name, domain);
                UserRepository.SetPassword(user, password);
                user.Properties.Email            = email;
                user.Properties.PasswordQuestion = passwordQuestion;
                user.Properties.PasswordAnswer   = passwordAnswer;
                user.Properties.Active           = isApproved;

                var membershipUser = new MembershipUser(user, Name);
                UpdateUserInRepository(user);
                status = MembershipCreateStatus.Success;
                return(membershipUser);
            } catch (UserNameAlreadyExistsException) {
                status = MembershipCreateStatus.DuplicateUserName;
            } catch (ArgumentException) {
                status = MembershipCreateStatus.InvalidUserName;
            }
            return(null);
        }
		public void UserName_CreateUserWithId_ProviderUserKeySameAsUserId() {
			var user = A.Fake<IUser>();
			A.CallTo(() => user.Id).Returns("userid");
			var memebershipUser = new MembershipUser(user, "provider");
			Assert.AreEqual("userid", memebershipUser.ProviderUserKey);
		}