The provided code is a C# program that demonstrates the usage of a JWT (JSON Web Token) authentication system. It includes the creation of a JWT container with user information, token generation, validation, and extraction of claims. The goal of this project was to dive deeper into API Authentication (a topic only touched on in my class). This repository is a bare and stripped down version of a token authentication system. This IS NOT production ready!
Program
is the main class containing the entry pointMain
method.- The program showcases the usage of JWT authentication.
- The
Main
method initializes a JWT container, generates a token using a JWT service, validates the token, and extracts and displays user claims if the token is valid.
- The JWT authentication is encapsulated in the
IAuth
andIAuthMethod
interfaces. GetJWT
method creates aJWT
object with user claims.
JWTService
is an implementation of theIAuthMethod
interface for JWT-based authentication.- It takes a secret key during initialization and provides methods for token generation, validation, and claim extraction.
- The
GetJWT
method creates aJWT
object with user claims (name and email). - The
JWTService
is initialized with the secret key obtained from theIAuth
object. - The program generates a token using the
GenerateToken
method of theIAuthMethod
interface. - The generated token is validated using the
ValidateToken
method. - If the token is valid, user claims (name and email) are extracted and displayed.
- If token validation fails, an
UnauthorizedAccessException
is thrown.