Ejemplo n.º 1
0
        public async Task <ActionResult <AuthenticateResponse> > AuthenticateByCredentials(AuthenticateRequest request)
        {
            if (string.IsNullOrEmpty(request.Username) || string.IsNullOrEmpty(request.Password))
            {
                return(BadRequest(new ErrorMessagerResponse()
                {
                    StatusCode = (int)HttpStatusCode.BadRequest, Message = "Invalid request parameters"
                }));
            }

            var authRequest = new AuthenticateByCredentialsRequest()
            {
                Username = request.Username, Password = request.Password
            };
            var response = await _authService.AuthenticateByCredentials(authRequest);

            if (response.AuthenticationStatus)
            {
                return new AuthenticateResponse()
                       {
                           User = response.User, AuthorizationToken = response.Token
                       }
            }
            ;

            return(Unauthorized(new ErrorMessagerResponse()
            {
                StatusCode = (int)HttpStatusCode.Unauthorized, Message = "Invalid Credentials"
            }));
        }
Ejemplo n.º 2
0
        public async Task <AuthenticateByCredentialsResponse> AuthenticateByCredentials(AuthenticateByCredentialsRequest request)
        {
            var response = new AuthenticateByCredentialsResponse();
            var user     = await _usersService.GetUserByUsernameAndPasswordAsync(request.Username, request.Password);

            if (user == null)
            {
                response.AuthenticationStatus = false;
                return(response);
            }

            var token = _authTokenService.GenerateJWTToken(user);

            return(new AuthenticateByCredentialsResponse()
            {
                User = user,
                Token = token,
                AuthenticationStatus = true
            });
        }