public async Task <ActionResult <LoginResultDto> > LoginAsync([FromBody] LoginRequestDto requestDto)
        {
            var user = await _userManager.FindByNameAsync(requestDto.UserName);

            LoginResultDto result;

            if (user != null && await _userManager.CheckPasswordAsync(user, requestDto.Password))
            {
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, user.UserName)
                };

                var token = _jwtTokenFactory.CreateToken(claims);
                result = new LoginResultDto {
                    Claims = claims, LoginSuccess = true, Token = token,
                };
            }
            else
            {
                result = new LoginResultDto {
                    LoginSuccess = false
                };
            }

            return(Ok(result));
        }
Exemple #2
0
        public async Task <Result <AuthenticatedTenantModel> > Handle(TenantAuthenticateCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation("New request with {logon} and {password}", request.Logon, request.Password);

            var secret = _appSettings.GetValue <string>("Secret");

            if (secret.Length < 15)
            {
                return(Result.Failure <AuthenticatedTenantModel>(ErrorType.SecretKeyTooShort.ToString()));
            }

            Maybe <Tenant> tenant = await _repository.SingleOrDefaultAsync(x => x.Logon.Equals(request.Logon), tracking : true, cancellationToken, x => x.Enterprise);

            if (tenant.HasNoValue)
            {
                return(Result.Failure <AuthenticatedTenantModel>(ErrorType.NotFound.ToString()));
            }

            var correctPassword = _hashing.IsValidHash(tenant.Value.Password, tenant.Value.Salt, request.Password);

            if (correctPassword is false)
            {
                return(Result.Failure <AuthenticatedTenantModel>(ErrorType.IncorrectUserPassword.ToString()));
            }

            var parsedExpiration = double.TryParse(_appSettings.GetValue <string>("TokenExpiration"), out var tokenExpiration);

            if (parsedExpiration is false)
            {
                tokenExpiration = 60;
            }

            tenant.Value.Token = _jwtTokenFactory.CreateToken(secret, tokenExpiration, tenant.Value.ID.ToString(), tenant.Value.Role.ToString(), tenant.Value.Enterprise.NormalizedEnterpriseName);

            return(Result.Success(_mapper.Map <AuthenticatedTenantModel>(tenant.Value)));
        }