Esempio n. 1
0
        public async Task <IActionResult> Post(Usuarios2 _userData)
        {
            Console.WriteLine("Estoy dentro");
            Console.WriteLine(_userData.NombreUsuario);
            Console.WriteLine(_userData.Contrasena);

            var prueba = await _TokenRepository.Authenticate(_userData.NombreUsuario, _userData.Contrasena);

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


            if (_userData != null && _userData.NombreUsuario != null && _userData.Contrasena != null)
            {
                var user = await _TokenRepository.Authenticate(_userData.NombreUsuario, _userData.Contrasena);

                Console.WriteLine(user.NombreUsuario);
                Console.WriteLine(user.Contrasena);

                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("NombreUsuario", user.NombreUsuario),
                        new Claim("Email", user.Email)
                        //  new Claim("UserName", user.UserName),
                        //  new Claim("Email", user.Email)
                    };

                    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());
            }
        }
        public async Task <IActionResult> Create(string username, string password)
        {
            SessionDto sessionDto = await _tokenRepository.Authenticate(username, password);

            if (sessionDto != null)
            {
                return(new ObjectResult(GenerateToken(sessionDto)));
            }

            return(BadRequest());
        }
Esempio n. 3
0
        public async Task <IActionResult> Token([FromForm] LoginModel loginModel)
        {
            Console.Write(loginModel.Username);
            Console.Write(loginModel.Password);
            var user = await _tokenRepo.Authenticate(loginModel.Username, loginModel.Password);

            if (user == null)
            {
                return(BadRequest());
            }
            return(Ok(user));
        }
Esempio n. 4
0
 public User Authenticate(string username, string password)
 {
     return(_tokenRepository.Authenticate(username, password));
 }