Esempio n. 1
0
        public async Task <Response <Guid> > Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var user = (await _userRepository.FindByCondition(x => x.UserId == request.UserId).ConfigureAwait(false)).AsQueryable().FirstOrDefault();

            user.FirstName    = request.FirstName;
            user.LastName     = request.LastName;
            user.UserEmail    = request.UserEmail;
            user.UpdatedBy    = request.UpdatedBy;
            user.UpdatedDate  = DateTime.UtcNow;
            user.UserStatuses = (await _userStatusRepository.FindByCondition(x => x.UserStatusId == request.UserStatus).ConfigureAwait(false)).AsQueryable().FirstOrDefault();

            var userObject = await _userRepository.UpdateAsync(user).ConfigureAwait(false);

            return(new Response <Guid>(userObject.UserId));
        }
Esempio n. 2
0
        public virtual async Task <Response <AuthenticationResponse> > AuthenticateAsync(AuthenticationRequest request, string ipAddress)
        {
            IList <ValidationFailure> errorMessages = new List <ValidationFailure>();

            var user = (await _UserRepository.FindByCondition(x => x.UserName.ToLower() == request.UserName.ToLower()).ConfigureAwait(false)).AsQueryable().FirstOrDefault();

            LoginLog log = new LoginLog()
            {
                LoginLogId     = Guid.NewGuid(),
                LoginDate      = DateTime.UtcNow,
                LoginUserIP    = ipAddress,
                UserIdentifier = request.UserName,
            };

            if (user == null)
            {
                errorMessages.Add(new ValidationFailure("UserName", $"No Accounts Registered with {request.UserName}."));
                log.LoginSuccess  = false;
                log.LoginLogTypes = (await _LoginLogTypeRepository.FindByCondition(x => x.LoginLogTypeName == LoginLogTypeId.IncorrectUserNameOrPassword.ToString()).ConfigureAwait(false)).AsQueryable().FirstOrDefault();
                await this._LoginLogRepository.AddAsync(log).ConfigureAwait(false);
            }

            var isBase64String = this._PasswordService.IsBase64String(request.Password);

            if (!isBase64String)
            {
                errorMessages.Add(new ValidationFailure("Password", $"Password is not base64 encoded."));
            }

            if (user != null)
            {
                var isPasswordValid = this._PasswordService.VerifyPasswordHash(this._PasswordService.Base64Decode(request.Password), Convert.FromBase64String(user.PasswordHash), Convert.FromBase64String(user.PasswordSalt));
                if (!isPasswordValid)
                {
                    errorMessages.Add(new ValidationFailure("Password", $"Password is Incorrect."));
                    log.LoginSuccess  = false;
                    log.LoginLogTypes = (await _LoginLogTypeRepository.FindByCondition(x => x.LoginLogTypeName == LoginLogTypeId.IncorrectUserNameOrPassword.ToString()).ConfigureAwait(false)).AsQueryable().FirstOrDefault();
                    await this._LoginLogRepository.AddAsync(log).ConfigureAwait(false);
                }
            }

            if (errorMessages.Count > 0)
            {
                throw new ValidationException(errorMessages);
            }

            log.LoginSuccess  = true;
            log.LoginLogTypes = (await _LoginLogTypeRepository.FindByCondition(x => x.LoginLogTypeName == LoginLogTypeId.Success.ToString()).ConfigureAwait(false)).AsQueryable().FirstOrDefault();
            await this._LoginLogRepository.AddAsync(log).ConfigureAwait(false);

            JwtSecurityToken jwtSecurityToken = await GenerateJWToken(user);

            var JwtToken = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);

            var userToken = GenerateRefreshToken(ipAddress);

            userToken.JwtToken = JwtToken;
            userToken.UserId   = user.UserId;

            user.UserTokens.Add(userToken);
            await _UserRepository.UpdateAsync(user).ConfigureAwait(false);

            AuthenticationResponse response = new AuthenticationResponse()
            {
                Id           = user.UserId.ToString(),
                Token        = JwtToken,
                Email        = user.UserEmail,
                UserName     = user.UserName,
                UserStatus   = user.UserStatuses.StatusValue,
                RefreshToken = userToken.Token,
            };

            return(new Response <AuthenticationResponse>(response, $"Authenticated {user.UserName}"));
        }