示例#1
0
        public async Task <IActionResult> Post(UserInfo _userData)
        {
            if (_userData != null && _userData.Email != null && _userData.Password != null)
            {
                var user = await GetUser(_userData.Email, _userData.Password);

                var ldapUser = authService.Login(user.UserName);


                if (null != ldapUser)
                {
                    try
                    {
                        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.UserId.ToString()),
                                new Claim("FirstName", user.FirstName),
                                new Claim("LastName", user.LastName),
                                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(365), signingCredentials: signIn);

                            return(Ok(new JwtSecurityTokenHandler().WriteToken(token)));
                        }
                        else
                        {
                            return(BadRequest("Invalid credentials"));
                        }
                    }
                    catch (Exception ex)
                    {
                        Funciones.Logs("TokenController", "Problemas al abrir la conexion; Captura error: " + ex.Message);
                        Funciones.Logs("TokenController", ex.StackTrace);
                        return(Unauthorized("en el catch"));
                    }
                }
                else
                {
                    return(Unauthorized("en el else de token controller"));
                }
            }
            else
            {
                return(BadRequest());
            }
        }
        //public async Task<ActionResult<IEnumerable<Lineas>>> GetLineas(string UserName, string Password)
        public async Task <IActionResult> Login(string UserName, string Password)
        {
            //var user = GetLineas(UserName="******", Password="******");

            //var user = authService.Login( "john.bernal","Carval2019b");
            var user = authService.Login("john.bernal");



            return((IActionResult)await _context.VentClienProductos.ToListAsync());



            //if (null != user)
            if (null != user)
            {
                try
                {
                    // create your login token here
                    //return (IActionResult)await _context.Vendedores.ToListAsync();
                    //return (IActionResult)user;
                    return((IActionResult)await _context.Lineas.ToListAsync());

                    //return Ok("debe estar bien " + user.UserName + " - " + user.Password);

                    //return await _context.Lineas.ToListAsync();
                }
                catch (Exception ex)
                {
                    Funciones.Logs("SecurityController", "Problemas al abrir la conexion; Captura error: " + ex.Message);
                    Funciones.Logs("SecurityController_DEBUG", ex.StackTrace);
                    return(Unauthorized("en el catch " + user));
                }
            }
            else
            {
                return(Unauthorized("en el else " + user));
            }
        }