Beispiel #1
0
        public ActionResult <string> Post([FromBody] User _userData)
        {
            Console.WriteLine(_userData.Username);
            Console.WriteLine(_userData.Password);
            var prueba = _tokenService.Authenticate(_userData.Username, _userData.Password);

            if (prueba == null)
            {
                Console.WriteLine("Comprobando...");
                return(Unauthorized(new { message = "Username or password is incorrect" }));
            }

            if (_userData != null && _userData.Username != null && _userData.Password != null)
            {
                var user = _tokenService.Authenticate(_userData.Username, _userData.Password);
                Console.WriteLine(user.Username);
                Console.WriteLine(user.Password);

                if (user != null)
                {
                    //create claims details based on the user information
                    var claims = new[] {
                        new Claim(JwtRegisteredClaimNames.Sub, _configuration["Jwt:Subject"]),
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                        new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()),
                        new Claim("Id", user.Id.ToString()),
                        new Claim("Username", user.Username),
                    };

                    var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));

                    var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                    var token = new JwtSecurityToken(_configuration["Jwt:Issuer"], _configuration["Jwt:Audience"], claims, expires: DateTime.UtcNow.AddDays(1), signingCredentials: signIn);

                    Console.WriteLine("Usuario Correcto, generando token...");
                    return(Ok(new
                    {
                        token = new JwtSecurityTokenHandler().WriteToken(token)
                    }));
                }
                else
                {
                    return(BadRequest("Invalid credentials"));
                }
            }
            else
            {
                return(BadRequest());
            }
        }
Beispiel #2
0
        public async Task <ViewUser> Login(string username, string password)
        {
            username.IsStringNotNullOrEmpty("Username");
            password.IsStringNotNullOrEmpty("Password");
            try
            {
                var account = await _accountRepository.Get(username);

                if (account == null)
                {
                    throw new InvalidLoginException("No account with the username: "******"password is incorrect");
                }
            }
            catch (NullReferenceException ex)
            {
                throw new InvalidLoginException("no user found");
            }
        }
Beispiel #3
0
        public IActionResult Authenticate([FromBody] SigninViewModel userCred)
        {
            var token = _tokenService.Authenticate(userCred.Username, userCred.Password);

            if (token == null)
            {
                return(Unauthorized());
            }
            return(Ok(token));
        }
Beispiel #4
0
        public async Task <ActionResult <AuthenticationResponse> > AuthenticateAsync(
            [FromBody] AuthenticationRequest request)
        {
            var result = await _tokenService.Authenticate(request);

            if (result is null)
            {
                return(BadRequest());
            }

            return(Ok(result));
        }
Beispiel #5
0
        public async Task <IActionResult> CreateToken([FromBody] LoginService login)
        {
            IActionResult response = Unauthorized();
            var           user     = await _token.Authenticate(login);

            if (user != null)
            {
                var tokenString = await _token.BuildToken(user);

                response = Ok(new { token = tokenString });
            }

            return(response);
        }
Beispiel #6
0
            /// <summary>
            ///     Handle
            /// </summary>
            /// <param name="command"></param>
            /// <param name="cancellationToken"></param>
            /// <returns></returns>
            public async Task <CommandResponse> Handle(AuthenticateCommand command, CancellationToken cancellationToken)
            {
                CommandResponse response = new CommandResponse();

                string ipAddress = _httpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

                TokenResponse tokenResponse = await _tokenService.Authenticate(command, ipAddress);

                if (tokenResponse == null)
                {
                    throw new InvalidCredentialsException();
                }

                response.Resource = tokenResponse;
                return(response);
            }