Core.SimpleJwtProvider
1.0.4
dotnet add package Core.SimpleJwtProvider --version 1.0.4
NuGet\Install-Package Core.SimpleJwtProvider -Version 1.0.4
<PackageReference Include="Core.SimpleJwtProvider" Version="1.0.4" />
paket add Core.SimpleJwtProvider --version 1.0.4
#r "nuget: Core.SimpleJwtProvider, 1.0.4"
// Install Core.SimpleJwtProvider as a Cake Addin #addin nuget:?package=Core.SimpleJwtProvider&version=1.0.4 // Install Core.SimpleJwtProvider as a Cake Tool #tool nuget:?package=Core.SimpleJwtProvider&version=1.0.4
How to use
This section will cover how to add the middleware to the StartUp DI.
You must also make sure of the following:
- Your application user must inherit TokenIdentityUser which itself inherits IdentityUser
- Your application Db Context must inherit from IdentityDbContext<T> with the TokenIdentityUser being the T
In the StartUp class do the following:
public void ConfigureServices(IServiceCollection services)
{
// Add your Db Context
services.AddDbContext<YOUR DB CONTEXT>();
// Configure the UserManager and RoleManager
services.AddIdentity<TokenIdentityUser, IdentityRole>()
.AddEntityFrameworkStores<YOUR DB CONTEXT>()
.AddDefaultTokenProviders();
var jwtConfigurationOptions = new JwtConfigurationOptions(
issuer: "YOUR ISSUER",
audience: "YOUR AUDIENCE",
secretKey: "YOUR SECRET KEY");
// For using the standard response body
services.AddJwtAuthentication(jwtConfigurationOptions);
// For overriding the response body for OnAuthentication Failed
services.AddJwtAuthentication(jwtConfigurationOptions, YourUnauthorizedResponseModel);
// For overriding the response body for both the OnAuthentication Failed and Forbidden
services.AddJwtAuthentication(jwtConfigurationOptions, YourUnauthorizedResponseModel, YourForbiddenResponseModel);
services.AddAuthorization();
services.AddDefaultJwtProvider();
// Other Stuff you want to add here
}
How to Generate a Token
In your class you just need to add the following to generate a token. The GenerateSymmetricTokenAsync method takes in the userId that the token is for as well as some options that need to be filled out. The method will then generate the token and add a refresh token to the users table. If there is any errors when creating a token the emthod will return a string.empty. The errors are then able to be found in a handy error list of the class called TokenErrors. Also logging is set up so if you have any logging middleware set up the default token provider will have an ILogger injected and will output to your logger of choice.
public class Foo
{
private readonly IJwtTokenProvider _provider;
public Foo(IJwtTokenProvider provider)
{
_provider = provider;
}
public async Task<string> GetTokenForUser(string userId)
{
var options = new JwtTokenOptions(
issuer: "YOUR ISSUER",
audience: "YOUR AUDIENCE",
secretKey: "YOUR SECRET KEY",
tokenExpiryInMinutes: 0,
refreshTokenExpiryInMinutes: 0,
addEmailToToken: true,
addUsernameToToken: true);
var tokenResult = await _provider.GenerateSymmetricTokenAsync(userId, options);
// There has been an error
if(result == string.empty)
{
// Get the errors and do something with them
var tokenErrors = _provider.TokenErrors;
}
else
{
return tokenResult;
}
}
}
Refresh Token
When a token needs to be refreshed due to it being Expired, the response will be a 401 Unauthorized. Contained within the Header of the Response will be a Key Value pair of "RefreshToken" and "true". If this does not exist in the response Header then it is a normal Unauthorized Response.
Like with the above section it requires some options to be input as well as a specific model. This model will contain:
- User Id
- Refresh Token
- Current Bearer Token
These are used internally to the default token provider to verify that the token being refreshed should be against that user. It will also check to see if the User has been set to locked out as well.
Like the previous section the refresh token method will return an string.empty if there is any errors and will set the TokenErrors property to the list of errors found. Also like the previous section, there is logging enabled.
public class Foo
{
private readonly IJwtTokenProvider _provider;
public Foo(IJwtTokenProvider provider)
{
_provider = provider;
}
public async Task<string> GetRefreshTokenForUser(string userId, string refreshToken, string currentBearerToken)
{
var refreshTokenModel = new JwtRefreshTokenModel(
userId: userId, // User Id of the user in the DB
refreshToken: refreshToken, // Refresh Token previously issued
currentBearerToken: currentBearerToken" // Current Bearer Token previously issued
);
var options = new JwtTokenOptions(
issuer: "YOUR ISSUER",
audience: "YOUR AUDIENCE",
secretKey: "YOUR SECRET KEY",
tokenExpiryInMinutes: 0,
refreshTokenExpiryInMinutes: 0,
addEmailToToken: true,
addUsernameToToken: true);
var tokenResult = await _provider.RefreshSymmetricSecurityTokenAsync(refreshTokenModel, options);
// There has been an error
if(tokenResult == string.empty)
{
// Get the errors and do something with them
var errors = _provider.TokenErrors;
return string.empty;
}
else
{
return tokenResult;
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Authentication (>= 2.2.0)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 3.1.3)
- Microsoft.Extensions.Identity.Stores (>= 3.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.