You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
914 B
29 lines
914 B
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
namespace API.Auth;
|
|
|
|
public static class Authentication
|
|
{
|
|
private static readonly TimeSpan TokenLifetime = TimeSpan.FromMinutes(15);
|
|
|
|
public static (string, DateTime) GenerateJwt(SymmetricSecurityKey key, IEnumerable<Claim> claims)
|
|
{
|
|
var expirationDate = DateTime.UtcNow.Add(TokenLifetime);
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(claims),
|
|
Expires = expirationDate,
|
|
SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
|
|
};
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
|
|
var jwt = tokenHandler.WriteToken(token);
|
|
return ("Bearer " + jwt, expirationDate);
|
|
}
|
|
|
|
|
|
} |