예제 #1
0
        public object Login(UsuarioLoginViewModel viewModel, Login login, Token token)
        {
            viewModel.Senha = CalculaHash(viewModel.Senha);
            var usuario = _mapper.Map <Usuario>(viewModel);
            var entrar  = _servicos.Login(usuario);

            bool usuarioValido = entrar != null;

            if (usuarioValido)
            {
                ClaimsIdentity identity = new ClaimsIdentity(
                    new GenericIdentity(entrar.Email, "Login"),
                    new[] {
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                    new Claim(JwtRegisteredClaimNames.UniqueName, entrar.Email)
                }
                    );

                DateTime dataCriacao   = DateTime.Now;
                DateTime dataExpiracao = dataCriacao + TimeSpan.FromSeconds(token.Segundos);

                var handler       = new JwtSecurityTokenHandler();
                var secutityToken = handler.CreateToken(new SecurityTokenDescriptor
                {
                    Issuer             = token.Emissor,
                    Audience           = token.Publico,
                    SigningCredentials = login.Credenciais,
                    Subject            = identity,
                    NotBefore          = dataCriacao,
                    Expires            = dataExpiracao
                });

                var accesstoken = handler.WriteToken(secutityToken);

                return(new
                {
                    authenticated = true,
                    created = dataCriacao.ToString("yyyy-MM-dd HH:mm:ss"),
                    expiration = dataExpiracao.ToString("yyyy-MM-dd HH:mm:ss"),
                    accessToken = accesstoken,
                    message = "OK"
                });
            }
            else
            {
                return(new
                {
                    authenticated = false,
                    message = "Falha ao autenticar"
                });
            }
        }
 public async Task <IActionResult> Login(string email, string senha)
 {
     return(Ok(await _servicos.Login(email, senha)));
 }