public override MembershipUser CreateUserSafe(string username, string password, string email, string passwordQuestion, string passwordAnswer,
            bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            if(password.Length < this.MinRequiredPasswordLength)
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);

            var args = new ValidatePasswordEventArgs(username, password, true);
            this.OnValidatingPassword(args);
            if(args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            var passwordSalt = PasswordUtil.CreateRandomSalt();
            var passwordHash = PasswordUtil.HashPassword(password.Trim(), passwordSalt);
            var user = new User
            {
                Username = username,
                PasswordSalt = passwordSalt,
                PasswordHash = passwordHash,
                Email = email,
                ApplicationName = this.ApplicationName,
                DateCreated = DateTime.UtcNow,
                IsApproved = isApproved
            };

            using(var session = this.DocumentStore.OpenSession())
            {
                session.Advanced.UseOptimisticConcurrency = true;

                try
                {
                    session.Store(user);
                    session.Store(new UniqueFieldConstraint(this.ApplicationName, EmailConstraintName, user.Email));

                    session.SaveChanges();

                    status = MembershipCreateStatus.Success;
                    return this.UserToMembershipUser(user, lastPasswordChangedDate: DateTime.UtcNow);
                }
                catch(ConcurrencyException e)
                {
                    status = this.InterpretConcurrencyException(user.Username, user.Email, e);
                }
                catch(Exception ex)
                {
                    this.LogException(ex);
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            return null;
        }
 private MembershipUser UserToMembershipUser(User user, DateTime? lastPasswordChangedDate = null)
 {
     var defaultDate = new DateTime(1900, 1, 1);
     return new RavenDBMembershipUser(
         providerName: this._providerName,
         username: user.Username,
         id: user.Id,
         email: user.Email,
         passwordQuestion: null,
         comment: null,
         isApproved: user.IsApproved,
         isLockedOut: false,
         creationDate: user.DateCreated,
         lastLoginDate: user.DateLastLogin ?? defaultDate,
         lastActivityDate: defaultDate,
         lastPasswordChangedDate: lastPasswordChangedDate ?? defaultDate,
         lastLockoutDate: defaultDate
     );
 }
Example #3
0
        public void StoreUserShouldCreateId()
        {
            var newUser = new User { Username = "******", FullName = "Martijn Boland" };

            using (var store = NewInMemoryStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(newUser);
                    session.SaveChanges();
                }
            }

            Assert.False(string.IsNullOrEmpty(newUser.Id));
        }