Code Monkey home page Code Monkey logo

forge.security.jwt.service's Introduction

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

forge.security.jwt.service's People

Contributors

jzo001 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.