mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
AuthController work
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Services;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace Govor.API.Services.Authentication;
|
||||
|
||||
public class JwtService : IJwtService
|
||||
{
|
||||
private JwtOption _jwtOption;
|
||||
|
||||
public JwtService(IOptions<JwtOption> options)
|
||||
{
|
||||
_jwtOption = options.Value;
|
||||
}
|
||||
public string GenerateJwtToken(User user)
|
||||
{
|
||||
Claim[] claims = [new("userID", user.Id.ToString())];
|
||||
|
||||
var singing = new SigningCredentials(
|
||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOption.SecretKeу)),
|
||||
SecurityAlgorithms.HmacSha256Signature);
|
||||
|
||||
var token = new JwtSecurityToken(signingCredentials: singing,
|
||||
expires: DateTime.UtcNow.AddHours(_jwtOption.Hours));
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user