97 lines
3.7 KiB
C#
97 lines
3.7 KiB
C#
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using FutureMailAPI.Models;
|
|
|
|
namespace FutureMailAPI.Helpers
|
|
{
|
|
public interface IJwtHelper
|
|
{
|
|
string GenerateToken(User user);
|
|
string GenerateToken(int userId, string username, string email);
|
|
ClaimsPrincipal? ValidateToken(string token);
|
|
}
|
|
|
|
public class JwtHelper : IJwtHelper
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public JwtHelper(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public string GenerateToken(User user)
|
|
{
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_configuration["Jwt:Key"]!);
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
|
new Claim(ClaimTypes.Name, user.Username),
|
|
new Claim(ClaimTypes.Email, user.Email)
|
|
}),
|
|
Expires = DateTime.UtcNow.AddDays(7),
|
|
Issuer = _configuration["Jwt:Issuer"],
|
|
Audience = _configuration["Jwt:Audience"],
|
|
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
|
};
|
|
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
return tokenHandler.WriteToken(token);
|
|
}
|
|
|
|
public string GenerateToken(int userId, string username, string email)
|
|
{
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_configuration["Jwt:Key"]!);
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
|
|
new Claim(ClaimTypes.Name, username),
|
|
new Claim(ClaimTypes.Email, email)
|
|
}),
|
|
Expires = DateTime.UtcNow.AddDays(7),
|
|
Issuer = _configuration["Jwt:Issuer"],
|
|
Audience = _configuration["Jwt:Audience"],
|
|
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
|
};
|
|
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
return tokenHandler.WriteToken(token);
|
|
}
|
|
|
|
public ClaimsPrincipal? ValidateToken(string token)
|
|
{
|
|
try
|
|
{
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_configuration["Jwt:Key"]!);
|
|
|
|
var validationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
ValidateIssuer = true,
|
|
ValidIssuer = _configuration["Jwt:Issuer"],
|
|
ValidateAudience = true,
|
|
ValidAudience = _configuration["Jwt:Audience"],
|
|
ValidateLifetime = true,
|
|
ClockSkew = TimeSpan.Zero
|
|
};
|
|
|
|
var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken validatedToken);
|
|
return principal;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
} |