https://github.com/jzo001/forge.security.jwt.service
Jwt Token based authentication / authorization service implementation
https://github.com/jzo001/forge.security.jwt.service
authentication authorization jwt jwt-authentication jwt-authorization jwt-bearer-tokens jwt-decode jwt-server jwt-token sso sso-authentication sso-login sso-server sso-solution
Last synced: 8 months ago
JSON representation
Jwt Token based authentication / authorization service implementation
- Host: GitHub
- URL: https://github.com/jzo001/forge.security.jwt.service
- Owner: JZO001
- License: mit
- Created: 2022-10-13T12:27:08.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-23T10:44:45.000Z (8 months ago)
- Last Synced: 2025-09-23T16:08:02.750Z (8 months ago)
- Topics: authentication, authorization, jwt, jwt-authentication, jwt-authorization, jwt-bearer-tokens, jwt-decode, jwt-server, jwt-token, sso, sso-authentication, sso-login, sso-server, sso-solution
- Language: C#
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```
```
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.
```c#
public async Task Login(string username, string password, IEnumerable 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