diff options
author | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-03-30 23:46:25 +0200 |
---|---|---|
committer | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-03-30 23:46:25 +0200 |
commit | be1d2a658726acaef3171ae3590f62d60f04792c (patch) | |
tree | e01b414e3ccc5c0db0f1788a11f55c3b87dcce89 /backend | |
parent | 39fc1f0cc9871b4436b839acb6ce4260e6c33931 (diff) |
Dodat TokenToId.
Diffstat (limited to 'backend')
-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() { |