Exemplo n.º 1
0
 public override bool ResetPasswordWithToken(string token, string newPassword)
 {
     if (string.IsNullOrEmpty(newPassword))
     {
         throw CreateArgumentNullOrEmptyException("newPassword");
     }
     using (PalaverDb context = new PalaverDb())
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token && Usr.PasswordVerificationTokenExpirationDate > DateTime.UtcNow);
         if (user != null)
         {
             dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
             if (newhashedPassword.Length > 128)
             {
                 throw new ArgumentException("Password too long");
             }
             user.Password                  = newhashedPassword;
             user.PasswordChangedDate       = DateTime.UtcNow;
             user.PasswordVerificationToken = null;
             user.PasswordVerificationTokenExpirationDate = null;
             context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Exemplo n.º 2
0
        public override string CreateAccount(string userName, string password, string email, bool requireConfirmationToken)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
            }
            string hashedPassword = CodeFirstCrypto.HashPassword(password);

            if (hashedPassword.Length > 128)
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
            }
            if (string.IsNullOrEmpty(userName))
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
            }
            if (string.IsNullOrEmpty(email))
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidEmail);
            }
            using (PalaverDb context = new PalaverDb())
            {
                dynamic user      = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
                dynamic emailuser = context.Users.FirstOrDefault(Usr => Usr.Email == email);
                if (user != null)
                {
                    throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
                }
                if (emailuser != null)
                {
                    throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateEmail);
                }
                string token = null;
                if (requireConfirmationToken)
                {
                    token = CodeFirstCrypto.GenerateToken();
                }
                int  defaultNumPasswordFailures = 0;
                User NewUser = new User
                {
                    UserId              = Guid.NewGuid(),
                    Username            = userName,
                    Password            = hashedPassword,
                    IsConfirmed         = !requireConfirmationToken,
                    Email               = email,
                    ConfirmationToken   = token,
                    CreateDate          = DateTime.UtcNow,
                    PasswordChangedDate = DateTime.UtcNow,
                    PasswordFailuresSinceLastSuccess = defaultNumPasswordFailures,
                    LastPasswordFailureDate          = DateTime.UtcNow
                };

                context.Users.Add(NewUser);
                context.SaveChanges();
                return(token);
            }
        }
Exemplo n.º 3
0
 public override bool ChangePassword(string userName, string oldPassword, string newPassword)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     if (string.IsNullOrEmpty(oldPassword))
     {
         throw CreateArgumentNullOrEmptyException("oldPassword");
     }
     if (string.IsNullOrEmpty(newPassword))
     {
         throw CreateArgumentNullOrEmptyException("newPassword");
     }
     using (PalaverDb context = new PalaverDb())
     {
         dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (user == null)
         {
             return(false);
         }
         dynamic hashedPassword        = user.Password;
         bool    verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, oldPassword));
         if (verificationSucceeded)
         {
             user.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int failures = user.PasswordFailuresSinceLastSuccess;
             if (failures != -1)
             {
                 user.PasswordFailuresSinceLastSuccess += 1;
                 user.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             context.SaveChanges();
             return(false);
         }
         dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword);
         if (newhashedPassword.Length > 128)
         {
             throw new ArgumentException("Password too long");
         }
         user.Password            = newhashedPassword;
         user.PasswordChangedDate = DateTime.UtcNow;
         context.SaveChanges();
         return(true);
     }
 }