Created
November 12, 2019 10:15
-
-
Save gpsarkar/5fdb9c52bca9b05aae9cc19f47c3dd5d to your computer and use it in GitHub Desktop.
get userid and tenant id from access token using JwtSecurityTokenHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static UserInfo GetClaimsFromToken(string accessTokenAsString) | |
{ | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
if (!(tokenHandler.ReadToken(accessTokenAsString) is JwtSecurityToken accessToken)) return null; | |
var claims = accessToken.Claims.ToList(); | |
var userId =claims.First(c => c.Type == "id").Value; // These could be different for your case | |
var tenantId = claims.First(c => c.Type == "tenant_id").Value; | |
return new UserInfo() | |
{ | |
Id = userId, | |
TenantId = tenantId | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment