0

I am getting a JWT-token in return from a ASP.NET Core API that contains Roles. I need to parse each role to be able to get the value, but have not been able to achieve it. Any ideas?

The property im trying to retrieve value for. There might me more than one role:

"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"

JWT to parse:

{
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "xxx",
  "UserId": "cd59f5eb-9477-4b7c-a978-7b1f60f83086",
  "http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "Administrator",
  "exp": 1543251220,
  "iss": "https://localhost:5000",
  "aud": "https://localhost:5000"
}

Parsing like:

let token = window.localStorage.getItem(ID_TOKEN_KEY)

    if (!token || token.split('.').length < 3) {
      return false
    }
    const data = JSON.parse(atob(token.split('.')[1]))

Trying to get property, but unable due to it being a URI:

 if (data.http://schemas.microsoft.com/ws/2008/06/identity/claims/role === 'Administrator') {
      return true
    }

1 Answers1

0

Properties that are complex strings need to be accessed like this:

if (data['http://schemas.microsoft.com/ws/2008/06/identity/claims/role'] === 'Administrator') {
  return true
}
Jim B.
  • 4,512
  • 3
  • 25
  • 53