private Task <ClaimsIdentity> GetClaims(AuthenticateUserComand command) { var customer = _repository.Get(command.UserName); var pass = Encripty.EncryptPassword(command.password).ToString().Substring(0, 12); if (customer == null) { return(Task.FromResult <ClaimsIdentity>(null)); } if (!(customer.UserName == command.UserName && customer.Password == pass)) { return(Task.FromResult <ClaimsIdentity>(null)); } _customer = customer; return(Task.FromResult(new ClaimsIdentity( new GenericIdentity(customer.UserName, "Token"), new[] { new Claim("TES", "User") } ))); }
public async Task <IActionResult> Post([FromForm] AuthenticateUserComand command) { if (command == null) { return(await Response(null, new List <Notification> { new Notification("User", "Usuário ou senha inválidos") })); } var identity = await GetClaims(command); if (identity == null) { return(await Response(null, new List <Notification> { new Notification("User", "Usuário ou senha inválidos") })); } var claims = new[] { new Claim(JwtRegisteredClaimNames.UniqueName, command.UserName), new Claim(JwtRegisteredClaimNames.Email, command.UserName), new Claim(JwtRegisteredClaimNames.Sub, command.UserName), new Claim(JwtRegisteredClaimNames.Jti, await _tokenOptions.JtiGenerator()), new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_tokenOptions.IssueAt).ToString(), ClaimValueTypes.Integer64), identity.FindFirst("ModernStore") }; var jwt = new JwtSecurityToken( issuer: _tokenOptions.Issuer, audience: _tokenOptions.Audience, claims: claims.AsEnumerable(), notBefore: _tokenOptions.NotBefore, expires: _tokenOptions.Expiration, signingCredentials: _tokenOptions.SigningCredentials); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); var response = new { token = encodedJwt, expires = (int)_tokenOptions.ValidFor.TotalSeconds, user = new { id = _customer.Id, name = _customer.Name.ToString(), email = _customer.Email, username = _customer.UserName } }; var json = JsonConvert.SerializeObject(response, _serializerSettings); return(new OkObjectResult(json)); }