Exemplo n.º 1
0
        public IActionResult AuthenticateUser(CredentialInput credentialInput)
        {
            var userInput = new UserInput(credentialInput.Email, credentialInput.Password);

            var hasher   = new SHA512Managed();
            var password = hasher.ComputeHash(Encoding.UTF8.GetBytes(credentialInput.Password));

            var user = _mapper.Map <User>(userInput);

            var jwtHandler = new JwtSecurityTokenHandler();
            var rawKey     = "12345678909876543";/*Configuration["Keys:rawKey"]*/
            var key        = Encoding.UTF8.GetBytes(rawKey);
            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim("Id", user.Id.ToString()),
                    new Claim("Name", user.Name)
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
            };
            var credentialToken = new CredentialTokenOutput
            {
                Id          = user.Id,
                AccessToken = jwtHandler.WriteToken(jwtHandler.CreateToken(securityTokenDescriptor)),
                Client      = ""
            };

            return(Ok(credentialToken));
        }
        public ResponseModelBase <LoginOutput> Login(CredentialInput credential)
        {
            var credentialModel = Mapper.Map <Credential>(credential);

            var result = _loginService.Login(credentialModel);

            var personOutput = Mapper.Map <LoginOutput>(result);

            return(new ResponseModelBase <LoginOutput>().OkResult(personOutput, result.Validations.ErrorMessages));
        }
        public ResponseModelBase <LoginOutput> Post([FromBody] CredentialInput credential)
        {
            var result = _loginAppService.Login(credential);

            if (!result.Error)
            {
                return(_tokenGenerator.CreateToken(result.Payload.FirstOrDefault()));
            }
            return(result);
        }