I needed a quick and easy way to generate a JWT which included some claims.
- Install these libraries
- Microsoft.IdentityModel.Tokens (6.17.0)
- System.IdentityModel.Tokens.Jwt (6.17.0)
- Add some configuration in appsettings, the
Secret
can be anything as long as its a key size of at least128
bits. I used this online GUID generator for testing.
1 | "IdentityToken": { |
- Build the service injecting the configuration
1 | using AuthService.Application.Common; |
- A use case could be the exchange of an Authorization Code. The example below is from my SPA, here I am sending the SPA’s
clientid
andclientsecret
along with thecode
theAuthorisation Service
returned at its login screen (that was the only time a username/password is required)
This is part of the Proof Key for Code Exchange (PKCE) flow (pronounced “pixy”).
1 | import { getAuthUrl, getClientId, getClientSecret } from '../common/EnvTools'; |
The Authorisation Service
could have a TokenController
with implementation as shown below.
1 | [ ] |
- The resulting response for this call could be
1 | { |
- Decoded using jwt.io the payload could look as follows, the specification on these properties is rfc7519.
Production JWTs should never be popped into any site like jwt.io, this is a security threat and you will get fired (again) -_-
Header
1 | { |
Payload
1 | { |
Details about the payload can be found at https://datatracker.ietf.org/doc/html/rfc7519#section-4.1 and OAuth2 - Delegation Token