47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using Empresa.App.Application.Interfaces.External;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Empresa.App.External.GetTokenJWT
|
|
{
|
|
public class GetTokenJwtService: IGetTokenJwtService
|
|
{
|
|
private readonly IConfiguration _config;
|
|
public GetTokenJwtService(IConfiguration config)
|
|
{
|
|
_config= config;
|
|
}
|
|
public string Execute(string id)
|
|
{
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
string key = _config["SecretKeyJwt"]??"";
|
|
var singinkey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
|
|
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject= new ClaimsIdentity(new Claim[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, id)
|
|
}),
|
|
Expires= DateTime.UtcNow.AddDays(1),//expira en un dia
|
|
SigningCredentials=new SigningCredentials(singinkey,SecurityAlgorithms.HmacSha256Signature),
|
|
Issuer = _config["IssuerJWT"],
|
|
Audience = _config["AudienceJwt"]
|
|
};
|
|
var token= tokenHandler.CreateToken(tokenDescriptor);
|
|
|
|
var tokenString = tokenHandler.WriteToken(token);
|
|
return tokenString;
|
|
|
|
|
|
}
|
|
}
|
|
}
|