コード例 #1
0
        public Response ChangePasswordUser(IUserRestartPasswordViewModel userModel)
        {
            var result = new Response();

            using (var context = ContextFactory())
            {
                try
                {
                    SafeManager.HashString(userModel.Password, out var hash, out var salt);
                    var user = context.Set <TEntity>().FirstOrDefault(w => w.Id == userModel.Id && !w.IsDeleted);
                    if (user != null)
                    {
                        var hashPassword = SafeManager.GetHashString(userModel.OldPassword, user.Salt);
                        if (user.Password.Equals(hashPassword))
                        {
                            if (userModel.OldPassword.Equals(userModel.Password))
                            {
                                result.AddError("Hasło jest takie samo jak poprzednie");
                            }
                            else
                            {
                                user.Password             = hash;
                                user.Salt                 = salt;
                                user.PasswordFailureCount = 0;
                                context.SaveChanges();
                                result.AddSucces("Hasło zostało zmienione.");
                            }
                        }
                        else
                        {
                            result.AddError("Złe hasło użytkownika");
                        }
                    }
                    else
                    {
                        result.AddError("Brak użytkownika");
                    }
                }
                catch (Exception e)
                {
                    //LoggerManager.Log(e);
                    //transaction.Rollback();
                    result.AddError("Bład zapisu");
                }
            }

            return(result);
        }
コード例 #2
0
        public LoginResult SignInManager(string login, string password)
        {
            var result = new LoginResult();

            using (var context = ContextFactory())
            {
                var user = context.Set <TEntity>().FirstOrDefault(w => w.Login == login && !w.IsDeleted);

                if (user != null && !user.IsBlock)
                {
                    if (user.PasswordFailureCount > 5)
                    {
                        result.SignInStatus = SignInStatus.Block;
                        user.IsBlock        = true;
                    }
                    else
                    {
                        var hashPassword = SafeManager.GetHashString(password, user.Salt);
                        if (user.Password.Equals(hashPassword))
                        {
                            SetAuthCookie(user);
                            user.PasswordFailureCount = 0;
                            result.SignInStatus       = SignInStatus.Ok;
                        }
                        else
                        {
                            user.LastPasswordFailureDate = DateTime.Now;
                            user.PasswordFailureCount   += 1;
                            result.SignInStatus          = user.PasswordFailureCount > 5
                                ? SignInStatus.Block
                                : SignInStatus.Failure;
                        }
                    }
                }
                else
                {
                    result.SignInStatus = SignInStatus.Failure;
                }

                context.SaveChanges();
            }

            return(result);
        }