JWT Authentication using C#

Clients security with JWT
Image that demonstrate what project we choose
Install JWT Nuget package in our project
  • Models folder — The folder will contain all the classes that stores only information.
  • Managers folder — This folder will store all the classes that makes the actual implementation of JWT..
Project setup visualization
IAuthService Interface
IAuthContainerModel interface
  • We will set default values for “ExpireMinutes” which will be 10080 minutes which is 7 days token expiration date.
  • We will use default encryption algorithm for “SecurityAlgorithm” which will be “HmacSha256Signature” and we get it from SecurityAlgorithms class.
  • For now I will set “SecretKey” value, it’s important to get the secret key from server configurations! putting the secret key as hard coded inside your class is really bad practice!
JWTContainerModel Class Implementation
  • The method GetSymmetricSecurityKey() takes the secret key and converts it to byte array and returns new SecurityKey class which sits in “Microsoft.IdentityModel.Tokens” namespace.
  • The method GetTokenValidationParameters() creates new instance of TokenValidationParameters model which sits in “Microsoft.IdentityModel.Tokens” namespace.
JWTService Class Implementation
IsTokenValid method implementation
GenerateToken method implementation
GetTokenClaims method implementation
GetJWTContainerModel method at Program class
Main method at Program class

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store