-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathJwtTokenService.cs
46 lines (42 loc) · 1.77 KB
/
JwtTokenService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using MyWarehouse.Infrastructure.Authentication.Model;
using MyWarehouse.Infrastructure.Authentication.Settings;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
namespace MyWarehouse.Infrastructure.Authentication.Services
{
public class JwtTokenService : ITokenService
{
private readonly AuthenticationSettings _authSettings;
public JwtTokenService(AuthenticationSettings authSettings)
{
_authSettings = authSettings;
}
public TokenModel CreateAuthenticationToken(string userId, string userName)
{
var expiration = DateTime.UtcNow.AddDays(7);
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(JwtRegisteredClaimNames.Iss, _authSettings.JwtIssuer),
new Claim(JwtRegisteredClaimNames.Aud, _authSettings.JwtAudience),
new Claim(JwtRegisteredClaimNames.Sub, userId),
new Claim(JwtRegisteredClaimNames.UniqueName, userName)
}),
Expires = expiration,
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(_authSettings.JwtSigningKey), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
var tokenString = tokenHandler.WriteToken(token);
return new TokenModel(
tokenType: "Bearer",
accessToken: tokenString,
expiresAt: expiration
);
}
}
}