31 lines
No EOL
871 B
C#
31 lines
No EOL
871 B
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using SurveyBackend.Core.Models;
|
|
|
|
namespace SurveyBackend.Infrastructure.Helpers;
|
|
|
|
public class TokenHelper
|
|
{
|
|
public static string GetAuthToken(User user)
|
|
{
|
|
var userId = user.Id.ToString();
|
|
|
|
var claims = new List<Claim>
|
|
{
|
|
new(ClaimTypes.NameIdentifier, userId)
|
|
};
|
|
|
|
var jwt = new JwtSecurityToken(
|
|
claims: claims,
|
|
issuer: AuthOptions.Issuer,
|
|
audience: AuthOptions.Audience,
|
|
expires: DateTime.UtcNow + AuthOptions.TokenLifetime,
|
|
signingCredentials: new SigningCredentials(AuthOptions.SymmetricSecurityKey, SecurityAlgorithms.HmacSha256)
|
|
);
|
|
|
|
var token = new JwtSecurityTokenHandler().WriteToken(jwt);
|
|
|
|
return token;
|
|
}
|
|
} |