Forge.Security.Jwt.Service 1.6.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Forge.Security.Jwt.Service --version 1.6.0                
NuGet\Install-Package Forge.Security.Jwt.Service -Version 1.6.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Forge.Security.Jwt.Service" Version="1.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Forge.Security.Jwt.Service --version 1.6.0                
#r "nuget: Forge.Security.Jwt.Service, 1.6.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Forge.Security.Jwt.Service as a Cake Addin
#addin nuget:?package=Forge.Security.Jwt.Service&version=1.6.0

// Install Forge.Security.Jwt.Service as a Cake Tool
#tool nuget:?package=Forge.Security.Jwt.Service&version=1.6.0                

Forge.Security.Jwt.Service

Forge.Security.Jwt.Service is a server side library that provides JWT (JSON Web Token) based authentication services.

Installing

To install the package add the following line to you csproj file replacing x.x.x with the latest version number:

<PackageReference Include="Forge.Security.Jwt.Service" Version="x.x.x" />

You can also install via the .NET CLI with the following command:

dotnet add package Forge.Security.Jwt.Service

If you're using Visual Studio you can also install via the built in NuGet package manager.

Usage

To use Forge.Security.Jwt.Service in service / server application check the example below.


public async Task<LoginResult> Login(string username, string password, IEnumerable<JwtKeyValuePair> secondaryKeys)
{
	await _signInManager.SignOutAsync();

	LoginResult result = new LoginResult();

	bool isExist = false;
	bool isAccountDisabled = false;
	using (DatabaseContext db = DatabaseContext.Create())
	{
		InititalizationAtStartup.IsUserAccountDisabled(db, username, out isExist, out isAccountDisabled);
	}

	if (isExist && !isAccountDisabled)
	{
		result.LoginResponse = new JwtTokenResult();

		User user = await _userManager.FindByNameAsync(username);
		SignInResult signInResult = await _signInManager.PasswordSignInAsync(user, password, false, false);
		result.Succeeded = signInResult.Succeeded;
		result.RequiresTwoFactor = signInResult.RequiresTwoFactor;
		result.IsLockedOut = signInResult.IsLockedOut;
		result.IsNotAllowed = signInResult.IsNotAllowed;

		var claims = new[]
		{
			new Claim(ClaimTypes.Name, username),
			new Claim(ClaimTypes.Role, user.Role),
			new Claim(ClaimTypes.NameIdentifier, user.Id),
			new Claim(ClaimTypes.Surname, user.Surname),
			new Claim(ClaimTypes.GivenName, user.Givenname),
			new Claim(ClaimTypes.Email, user.Email),
		};

		JwtTokenResult jwtResult = _jwtAuthManager.GenerateTokens(username, claims, DateTime.UtcNow, secondaryKeys);
		result.LoginResponse = jwtResult;
	}

	return result;
}

About the incoming parameters:

  • username and password are the standard user credentials
  • secondaryKeys is a set of additional keys or attributes which makes every users as unique as possible on the client side. It is not mandatory to use it, but it is recommended to put additional info, like IP address and/or user-agent, etc.

You can find a full reference implementation in project "Forge.Yoda". Please feel free to check the project in my repositories. In the solution, "Forge.Yoda.Services.Authentication" is what you are looking for.

Please also check the following projects in my repositories:

  • Forge.Yoda
  • Forge.Security.Jwt.Service
  • Forge.Security.Jwt.Service.Storage.SqlServer
  • Forge.Security.Jwt.Client
  • Forge.Security.Jwt.Client.Storage.Browser
  • Forge.Wasm.BrowserStorages
  • Forge.Wasm.BrowserStorages.NewtonSoft.Json
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.1 248 12/5/2023
1.6.0 176 5/25/2023
1.5.6 151 5/19/2023
1.5.5 166 5/19/2023
1.5.4 146 5/16/2023
1.5.3 385 11/21/2022
1.5.2 379 10/30/2022
1.5.1 413 10/24/2022