diff options
-rw-r--r-- | backend/api/api/Models/IJwtToken.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Models/JwtToken.cs | 25 |
2 files changed, 26 insertions, 0 deletions
diff --git a/backend/api/api/Models/IJwtToken.cs b/backend/api/api/Models/IJwtToken.cs index da71f7ec..2afb6683 100644 --- a/backend/api/api/Models/IJwtToken.cs +++ b/backend/api/api/Models/IJwtToken.cs @@ -8,5 +8,6 @@ namespace api.Models string GenToken(AuthRequest user); string RenewToken(string existingToken); string TokenToUsername(string token); + public string TokenToId(string token); } }
\ No newline at end of file diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index 29f4bafc..06b3a666 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -74,6 +74,31 @@ namespace api.Models } } + public string TokenToId(string token) + { + if (token == null) + return null; + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + try + { + tokenHandler.ValidateToken(token, new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false, + }, out SecurityToken validatedToken); + + var jwtToken = (JwtSecurityToken)validatedToken; + return jwtToken.Claims.First(x => x.Type == "id").Value; + } + catch + { + return null; + } + + } public string GenGuestToken() { |