using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using UsuariosApi.Models;
namespace UsuariosApi.Services
{
public class TokenServices
{
public string GenerateToken(Usuario usuario)
{
Claim[] claims = new Claim[]
{
new Claim("username", usuario.UserName),
new Claim("id", usuario.Id),
new Claim(ClaimTypes.DateOfBirth, usuario.DataNascimento.ToString()),
new Claim("loginTimestamp", DateTime.UtcNow.ToString())
};
var chave = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("9ASHDA98H9ah9ha9H9A89n0f"));
var signingCredenials = new SigningCredentials(chave, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken
(
expires: DateTime.Now.AddMinutes(10),
claims: claims,
signingCredentials: signingCredenials
);
return new JwtSecurityTokenHandler().WriteToken(token);
}
}
}