Ejemplo n.º 1
0
        public async Task <IdentityResult> ChangePasswordFromTokenAsync(int userId, string passwordResetToken, string newPassword)
        {
            var user = await this.FindByIdAsync(userId).ConfigureAwait(false);

            if (user.PasswordResetToken != passwordResetToken || !user.PasswordResetExpiry.HasValue || user.PasswordResetExpiry < DateTime.Now)
            {
                return(new IdentityResult("Your password reset token has expired or does not exist"));
            }
            var securedPassword = new SecuredPassword(newPassword);

            if (securedPassword.Verify(newPassword))
            {
                user.PasswordHash            = Convert.ToBase64String(securedPassword.Hash);
                user.Salt                    = Convert.ToBase64String(securedPassword.Salt);
                user.PasswordResetExpiry     = null;
                user.PasswordResetToken      = null;
                user.FailedLogonAttemptCount = 0;
                user.UserLogs.Add(new UserLog()
                {
                    Description = "Password reset using token"
                });
            }
            await this.dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(new IdentityResult());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Finds the user from the password, if the password is incorrect then increment the number of failed logon attempts
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <LogonResult> FindAndCheckLogonAsync(string userName, string password)
        {
            var user = await this.dbContext.User.SingleOrDefaultAsync(u => u.UserName == userName && u.Enabled && u.Approved && u.EmailVerified).ConfigureAwait(false);

            var logonResult = new LogonResult();

            if (user != null)
            {
                var  securedPassword = new SecuredPassword(Convert.FromBase64String(user.PasswordHash), Convert.FromBase64String(user.Salt));
                bool checkFailedLogonAttemptCount   = Convert.ToBoolean(ConfigurationManager.AppSettings["AccountManagementCheckFailedLogonAttemptCount"].ToString());
                int  maximumFailedLogonAttemptCount = Convert.ToInt32(ConfigurationManager.AppSettings["AccountManagementMaximumFailedLogonAttemptCount"].ToString());
                if (checkFailedLogonAttemptCount == false || user.FailedLogonAttemptCount < maximumFailedLogonAttemptCount)
                {
                    if (securedPassword.Verify(password))
                    {
                        user.FailedLogonAttemptCount = 0;
                        this.dbContext.SaveChanges();
                        logonResult.Success  = true;
                        logonResult.UserName = user.UserName;
                        return(logonResult);
                    }
                    else
                    {
                        user.FailedLogonAttemptCount       += 1;
                        logonResult.FailedLogonAttemptCount = user.FailedLogonAttemptCount;
                        user.UserLogs.Add(new UserLog()
                        {
                            Description = "Failed Logon attempt"
                        });
                        this.dbContext.SaveChanges();
                    }
                }
            }
            return(logonResult);
        }
Ejemplo n.º 3
0
        public async Task <int> ChangePasswordAsync(int userId, string currentPassword, string newPassword)
        {
            var user = await this.FindByIdAsync(userId).ConfigureAwait(false);

            var securedPassword = new SecuredPassword(currentPassword);

            if (securedPassword.Verify(currentPassword))
            {
                user.PasswordHash = Convert.ToBase64String(securedPassword.Hash);
                user.Salt         = Convert.ToBase64String(securedPassword.Salt);
            }
            user.UserLogs.Add(new UserLog()
            {
                Description = "Password changed"
            });
            return(await this.dbContext.SaveChangesAsync().ConfigureAwait(false));
        }