コード例 #1
0
        //Generate password salt and hased password
        internal async Task <bool> GenerateHashedPassword(RegistrationRequestModel request)
        {
            bool result = false;

            _user = new AppUser();

            try
            {
                var hashed = await _crypto.EncryptPassword(request.Password);

                if (hashed?.Count == 0)
                {
                    _logger.Warning("Hashed password is empty");
                    return(result);
                }

                _user.PasswordSalt   = Convert.ToBase64String(hashed.FirstOrDefault().Value);
                _user.HashedPassword = hashed.FirstOrDefault().Key;
                result = true;
            }
            catch (Exception e)
            {
                IsError = true;
                _logger.Error(e, "Failed to generate hashed password");
            }

            return(result);
        }
コード例 #2
0
        public void Create(User user)
        {
            try
            {
                user.Password = Crypto.EncryptPassword(user.Password);

                Ctx.Users.Add(user);
                Ctx.SaveChanges();

                Logger.LogInformation($"Salvando usuário -> {user.UserId}");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                throw;
            }
        }
コード例 #3
0
        /// <summary>
        /// Use Encryption to generate the hashed password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="plainPassword"></param>
        /// <returns></returns>
        internal async Task <string> GetHashedPassword(string username, string plainPassword)
        {
            string result = null;

            var salt = GetPasswordSalt(username);

            try
            {
                result = (await _crypto.EncryptPassword(plainPassword, Convert.FromBase64String(salt))).FirstOrDefault().Key;
            }
            catch (Exception e)
            {
                IsError = true;
                _logger.Error(e, "Failed to get hashed password");
            }

            return(result);
        }