using Contracts.BindingModels; using Contracts.ViewModels; using Microsoft.IdentityModel.Tokens; using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Text; using System.Threading.Tasks; namespace BusinessLogic.Tools { public class JwtProvider { private static string _key; private static int _expiresHours; public static string Generate(UserBindingModel user) { var signingCredentials = new SigningCredentials( new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_key)), SecurityAlgorithms.HmacSha256); Claim[] claims = [ new("userId", user.Id.ToString()), new("role", user.Role.Name) ]; var token = new JwtSecurityToken(signingCredentials: signingCredentials, expires: DateTime.UtcNow.AddHours(_expiresHours), claims: claims); var stringToken = new JwtSecurityTokenHandler().WriteToken(token); return stringToken; } public void SetupJwtOptions(JwtOptions options) { _key = options.SecretKey; _expiresHours = options.ExpiresHours; } } }