public async Task <ResultResponse <LoggedUserDto> > RegisterAsync(RegisterRequest request) { var user = await _userRepository.GetByEmailAsync(request.Email); if (user != null) { return(new ResultResponse <LoggedUserDto>("User with this email is already exist")); } var passwordSalt = _encryptionService.CreateSalt(); var passwordHash = _encryptionService.CreateHash(request.Password, passwordSalt); user = new User { FirstName = request.FirstName, LastName = request.LastName, Email = request.Email, Status = UserStatuses.Verified, Role = request.Role, PasswordHash = passwordHash, PasswordSalt = passwordSalt, RefreshToken = _tokenService.GenerateRefreshToken() }; await _userRepository.AddAsync(user); await _unitOfWork.SaveChangesAsync(); var token = _tokenService.GenerateToken(user.Email); var loggedUserDto = _mapper.Map <User, LoggedUserDto>(user); loggedUserDto.Token = token; return(new ResultResponse <LoggedUserDto>(loggedUserDto)); }
public void Should_Generate_Hash() { var hashString = "password"; var salt = _encryptionService.CreateSalt(); var hash = _encryptionService.CreateHash(hashString, salt); Assert.NotNull(hash); Assert.Equal(HashLength, hash.Length); }
public Result <UserSession> CreateAccessToken(int userID) { if (_accountAccessor.FindUser(userID) == null) { return(new Result <UserSession>(false, "Invalid UserID received.")); } var accessToken = _encryptionService.CreateHash(Encoding.UTF8.GetBytes(DateTime.Now.ToString() + userID), _configuration["HashCode"]); var expiresOn = DateTime.Now.AddDays(14); return(_accountAccessor.CreateAccessToken(userID, accessToken, expiresOn)); }
public async Task <BaseResponse> RegisterAsync(RegisterRequest request) { var user = await _userRepository.GetAsync(user => user.Email == request.Email); if (user != null) { return(new ResultResponse <LoggedUserDto>("User with this email is already exist")); } var passwordSalt = _encryptionService.CreateSalt(); var passwordHash = _encryptionService.CreateHash(request.Password, passwordSalt); user = new User { FullName = request.FullName, Email = request.Email, PasswordHash = passwordHash, PasswordSalt = passwordSalt, }; await _userRepository.AddAsync(user); await _unitOfWork.SaveChangesAsync(); return(new BaseResponse()); }
public bool ValidatePass(string password) { var allHashs = _authRepository.Table.ToList(); if (allHashs.Any()) { return(allHashs.Any(authDomain => _encryptionService.Validate(password, authDomain.PassHash))); } // for first app run _authRepository.Insert(new AuthDomain() { Id = 1, PassHash = _encryptionService.CreateHash(password) }); return(true); }
///<summary> /// 检查应用接入的数据完整性 /// </summary> /// <param name="signature">加密签名内容</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机字符串</param> /// <param name="appid">应用接入Id</param> /// <returns></returns> protected bool ValidateSignature(string signature, string timestamp, string nonce, string appid) { string[] ArrTmp = { "club", _securitySettings.EncryptionKey, timestamp, nonce, appid }; Array.Sort(ArrTmp); string tmpStr = string.Join("", ArrTmp); tmpStr = _encryptionService.CreateHash(Encoding.UTF8.GetBytes(tmpStr), "SHA1"); tmpStr = tmpStr.ToLower(); long timestampInt; if (tmpStr == signature && long.TryParse(timestamp, out timestampInt)) { return(true); } else { return(false); } }
public bool PasswordMatch(CustomerPassword customerPassword, string enteredPassword) { switch (_customerSettings.DefaultPasswordFormat) { case PasswordFormat.Clear: return(customerPassword.Password.Equals(enteredPassword, StringComparison.InvariantCulture)); case PasswordFormat.Encrypted: return(_encryptionService.DecryptText(customerPassword.Password).Equals(enteredPassword)); case PasswordFormat.Hashed: return(customerPassword.Password.Equals( _encryptionService.CreateHash(enteredPassword, _customerSettings.HashedPasswordFormat), StringComparison.InvariantCultureIgnoreCase)); default: return(false); } }
/// <summary> /// Creates the hash sum of identifiers list /// </summary> /// <param name="ids"></param> /// <returns></returns> public virtual string CreateIdsHash(IEnumerable <int> ids) { return(_encryptionService.CreateHash(Encoding.UTF8.GetBytes(string.Join(", ", ids.OrderBy(id => id))), NopCustomerServiceDefaults.DefaultHashedPasswordFormat)); }
/// <summary> /// Create a data hash /// </summary> /// <param name="data">The data for calculating the hash</param> /// <param name="hashAlgorithm">Hash algorithm</param> /// <returns>Data hash</returns> public string CreateHash(byte[] data, string hashAlgorithm = "SHA1") { return(_encryptionService.CreateHash(data, hashAlgorithm)); }