massive work on user auth
This commit is contained in:
parent
35331a87f1
commit
c2bcaf0832
17 changed files with 186 additions and 19 deletions
|
|
@ -0,0 +1,31 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue