mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 22:10:05 +01:00
35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
|
|
namespace QuotifyBE.Controllers;
|
|
|
|
public class GeneralUseHelpers
|
|
{
|
|
public string GenerateJwtToken(string username, IConfiguration appsettings)
|
|
{
|
|
var claims = new[]
|
|
{
|
|
new Claim(JwtRegisteredClaimNames.Sub, username),
|
|
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
|
};
|
|
|
|
var key = new SymmetricSecurityKey(
|
|
Encoding.UTF8.GetBytes(appsettings["JwtSecret"]!)
|
|
// won't be null here - otherwise Program.cs wouldn't start
|
|
);
|
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|
|
|
var token = new JwtSecurityToken(
|
|
issuer: appsettings["DomainName"]!,
|
|
audience: appsettings["DomainName"]!,
|
|
claims: claims,
|
|
expires: DateTime.Now.AddDays(7),
|
|
signingCredentials: creds
|
|
);
|
|
|
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
}
|
|
}
|