using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using USEntryCoach.Server.Data; using USEntryCoach.Server.Settings; namespace USEntryCoach.Server.Services; public class TokenService(IOptions authenticationSettings) { public string GenerateToken(User user) { JwtSecurityTokenHandler tokenHandler = new(); SecurityTokenDescriptor tokenDescriptor = new() { Subject = new ClaimsIdentity([ new Claim(ClaimTypes.Name, user.Username), new Claim(ClaimTypes.Role, user.Role.ToString()) ]), Expires = DateTime.UtcNow.Add(authenticationSettings.Value.JwtExpiryTime), SigningCredentials = new SigningCredentials( new SymmetricSecurityKey(authenticationSettings.Value.JwtGenerationSecretBytes), SecurityAlgorithms.HmacSha256Signature) }; SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } }