File tree Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,6 @@ public TokenService(IConfiguration configuration)
20
20
public string GenerateToken ( User user )
21
21
{
22
22
IConfigurationSection jwtSettings = _configuration . GetSection ( "JwtSettings" ) ;
23
-
24
23
SymmetricSecurityKey key = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( jwtSettings [ "Key" ] ! ) ) ;
25
24
SigningCredentials credentials = new SigningCredentials ( key , SecurityAlgorithms . HmacSha256 ) ;
26
25
@@ -34,10 +33,16 @@ public string GenerateToken(User user)
34
33
claims . Add ( new Claim ( ClaimTypes . Role , user . Role . Name ) ) ;
35
34
}
36
35
36
+ string ? expiryMinutes = jwtSettings [ "ExpiryMinutes" ] ;
37
+ if ( string . IsNullOrEmpty ( expiryMinutes ) || ! double . TryParse ( expiryMinutes , out double minutes ) )
38
+ {
39
+ throw new InvalidOperationException ( "JWT ExpiryMinutes is not properly configured" ) ;
40
+ }
41
+
37
42
SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
38
43
{
39
44
Subject = new ClaimsIdentity ( claims ) ,
40
- Expires = DateTime . UtcNow . AddMinutes ( Convert . ToDouble ( jwtSettings [ "ExpiryMinutes" ] ! ) ) ,
45
+ Expires = DateTime . UtcNow . AddMinutes ( minutes ) ,
41
46
Issuer = jwtSettings [ "Issuer" ] ,
42
47
Audience = jwtSettings [ "Audience" ] ,
43
48
SigningCredentials = credentials
You can’t perform that action at this time.
0 commit comments