Auth.JWT
2.0.0
See the version list below for details.
dotnet add package Auth.JWT --version 2.0.0
NuGet\Install-Package Auth.JWT -Version 2.0.0
<PackageReference Include="Auth.JWT" Version="2.0.0" />
paket add Auth.JWT --version 2.0.0
#r "nuget: Auth.JWT, 2.0.0"
// Install Auth.JWT as a Cake Addin #addin nuget:?package=Auth.JWT&version=2.0.0 // Install Auth.JWT as a Cake Tool #tool nuget:?package=Auth.JWT&version=2.0.0
Auth JWT
What is JWT (Json Web Token)?
JSON Web Token (JWT) is a token based authentication between two servers. In this a JSON object that
is digitally signed using JSON Web Signature (JWS) and encrypted using JSON Web Encryption (JWE).
Authentication sequence for JWT
What is Auth JWT?
Auth JWT is a helper to achieve JWT authentication in ASP.Net Core applications in an easy and flexible
way.
Hashing Algorithms Used
➢ SHA1
➢ SHA256
➢ SHA384
➢ SHA512
Components of Auth JWT
➢ TokenRequestModel is a class which contain properties for Auth JWT payload.
o [Note: All properties are not mandatory.]
• Issuer
• ExpiryInSeconds
• UserId
• User
• Role
• Audience
• JwtId
• Subject
• CustomProperty
AlgorithmType is constant class to get the algorithm details
• SHA1
• SHA256
• SHA384
• SHA512
JWTModule is a class which contains all the interfaces for Auth JWT.
• CreateToken(<TokenRequestModel>, <secrect>, <AlgoritmType>) → Function creates
the token
• VerifyToken(<token>, <secrect>, <ValidateModel Optional>) → Function verifies
the token.
TokenResponseModel is a class which contains the Status and Content of the Auth
JWT response.
• Status
• Content
ValidateModel is a class which contain properties to be verified in the
VerifyToken() function.
o
[Note: All properties should match the TokenRequestModel.]
• Issuer
• UserId
• User
• Role
• Audience
• JwtId
• Subject
• CustomProperty
Configuration of Auth.JWT
Step 1:
Add the service to the ConfigureServices(IServiceCollection services) function.
services.AddAuthService();
Step 3
Implement Constructor injection in the required classes, to get the object of Auth.JWT.
Sample Code Snippet
In the below code, I have used Auth.JWT to create and validate the token in a simple class for better
understanding.
class TestAuthJwt
{
private readonly JWTModule _module;
private readonly TokenRequestModel _reqModel;
private readonly ValidateModel _validateModel;
public TestAuthJwt(JWTModule module, TokenRequestModel reqModel, ValidateModel
validateModel)
{
_module = module;
_reqModel = reqModel;
_validateModel = validateModel;
}
public void Execute()
{
string secrect = "F4760D";
_reqModel.Issuer = "authjwt_team";
_reqModel.ExpiryInSeconds = "1000";
_reqModel.UserId = "U1324322";
_reqModel.User = "sambeet";
_reqModel.Role = "admin";
_reqModel.Audience = "authjwt_app";
_reqModel.JwtId = "J4433421";
_reqModel.Subject = "authjwt_subject";
_reqModel.CustomProperty.Add("CustomField1","auth_custom1");
_reqModel.CustomProperty.Add("CustomField2", "auth_custom2");
var result = _module.CreateToken(_reqModel, secrect, AlgorithmType.SHA256);
Console.WriteLine("************* Create Token Result***************");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result));
_validateModel.Issuer = "authjwt_team";
_validateModel.UserId = "U1324322";
_validateModel.User = "sambeet";
_validateModel.Role = "admin";
_validateModel.Audience = "authjwt_app";
_validateModel.JwtId = "J4433421";
_validateModel.Subject = "authjwt_subject";
_validateModel.CustomProperty.Add("CustomField1", "auth_custom1");
_validateModel.CustomProperty.Add("CustomField2", "auth_custom2");
var verifyResult = _module.VerifyToken(result.Content, secrect, _validateModel);
Console.WriteLine("************* Verify Token Result***************");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(verifyResult));
Console.ReadKey();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Following is the user manual:
-------------------------------------
https://1drv.ms/b/s!AhdXGbbVEmBIhAkDCzDoqnpuWdvP?e=7SdFq8