public async Task <IActionResult> Authenticate(Authenticate.Request request, CancellationToken cancellationToken) { try { return(Ok(await _mediator.Send(request, cancellationToken))); } catch (UsernamePasswordMismatchException e) { return(new UnauthorizedObjectResult(new { e.Message })); } }
public async Task <IActionResult> Authenticate([FromBody] Authenticate.Request dto) { //Valida o Client informado if (!this.configurationService.GetSection("Authentication:Clients").Get <string[]>().Contains(dto.ClientId)) { return(new UnauthorizedResult()); } var authenticationResult = this.segurancaService.Authenticate(dto); if (authenticationResult == null) { return(BadRequest()); } return(Ok(authenticationResult)); }
public async Task <ActionResult <Authenticate.Response> > Token([FromBody] Authenticate.Request request) { return(await _mediator.Send(request)); }
public Authenticate.Response Authenticate(Authenticate.Request dto) { //Recupera o usuário var usuarioModel = this.ConsultaUsuario(new EFTJUserweb() { Login = dto.Login }); if (usuarioModel == null) { usuarioModel = this.ConsultaUsuario(new EFTJUserweb() { CPF = dto.Login }); } if (usuarioModel == null) { return(null); } var password = String.Join("", System.Security.Cryptography.SHA1.Create().ComputeHash( Encoding.UTF8.GetBytes( String.Concat(usuarioModel.pwdKey, dto.Password) ) ).Select(x => x.ToString("X2"))).ToLower(); //Valida o Usuário e Senha this.Login(new SGDAU.Seguranca.Domain.Models.EFTJUserweb() { Login = dto.Login, PassWord = password }); var jwtData = new JwtData() { AllocatedVaraID = usuarioModel.VaraAlocacao, BusinessUnitID = usuarioModel.EFTJUnidadeID, PrinterID = usuarioModel.EFTJImpressoraID, RegionID = usuarioModel.EFRegiaoID, GroupID = usuarioModel.EFGrupoID, CategoryID = usuarioModel.Categoria, Name = usuarioModel.Nome, Login = dto.Login, UserID = usuarioModel.EFUserID, ClientID = dto.ClientId }; //Calcula o hash de validação com os dados do usuário jwtData.Hash = JwtData.CalculateHash(this.configurationService, jwtData); //Gera o token JWT var audience = dto.ClientId; var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(this.configurationService.GetSection("Authentication:SecretKey").Value); var tokenDescriptor = new SecurityTokenDescriptor { Issuer = this.configurationService.GetSection("Authentication:IssuerName").Value, IssuedAt = DateTime.UtcNow, NotBefore = DateTime.UtcNow, Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, usuarioModel.Nome), new Claim(ClaimTypes.UserData, Newtonsoft.Json.JsonConvert.SerializeObject(jwtData)) }), Expires = DateTime.UtcNow.AddHours(8), Audience = audience, SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; ////Limpa a lista de Acessos do UserData //jwtData.AccessPermissions = new AccessDTO[0]; return(new Authenticate.Response() { UserData = jwtData, Token = tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor)) }); }