Buzzytom.Extensions.Authentication 1.0.1

dotnet add package Buzzytom.Extensions.Authentication --version 1.0.1
NuGet\Install-Package Buzzytom.Extensions.Authentication -Version 1.0.1
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="Buzzytom.Extensions.Authentication" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Buzzytom.Extensions.Authentication --version 1.0.1
#r "nuget: Buzzytom.Extensions.Authentication, 1.0.1"
#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 Buzzytom.Extensions.Authentication as a Cake Addin
#addin nuget:?package=Buzzytom.Extensions.Authentication&version=1.0.1

// Install Buzzytom.Extensions.Authentication as a Cake Tool
#tool nuget:?package=Buzzytom.Extensions.Authentication&version=1.0.1

Extensions.Authentication

Lean token extensions for issuing/validating tokens, automatically creating a private key and creating password salts. Compatible with Asp.Net Core 2.1.

Usage Guide

Install the NuGet package

Buzzytom.Extensions.Authentication

Add using statements to get the extensions.

using Extensions.Authentication;
using Extensions.Authentication.JwtBearer;

In the ConfigureServices of your application (or equivalent) add the following calls to register the required services and middleware.

public void ConfigureServices(IServiceCollection services)
{
    // Register other services here, like entity framework
    
    // Adds a ITokenService to the dependency services
    services.AddTokenService();
	
    // Adds a IValidatorService to the dependency services
    services.AddValidationService();
	
    // Adds:
    // - JWT middleware services supporting the Authorize attribute
    // - Adds a ISymmetricKeyProvider to the dependency services
    services.AddJwt();
}

Make sure your application is configured to use authentication.

public void Configure(IApplicationBuilder application)
{
    // Configure other services here

    // Enable authentication, JWT middleware does not work without this
    application.UseAuthentication();
	
    // Configure other services here. E.g. services.UseMvc();
}

Example controller to issue authentication tokens.

[Route("/api/account")]
public class AuthenticationController : ControllerBase
{
	private readonly ITokenService tokenService;
	private readonly IBearerTokenService bearerTokenService;

	public AuthenticationController(ITokenService tokenService,
                                    IBearerTokenService, bearerTokenService)
	{
        this.tokenService = tokenService;
        this.bearerTokenService = bearerTokenService;
	}

	[HttpPost]
	[Route("authenticate")]
	public string Authenticate([FromBody] AuthenticateRequest request)
	{
        // DON'T ACTUALLY DO IT LIKE THIS
        // THIS IS ONLY AN EXAMPLE
		
        YourUserType user = GetUserFromYourPersistentStore(request.Email);
	
        string requestHash = tokenService.Hash(request.Password, user.Salt);
        if (requestHash == user.PasswordHash)
            return bearerTokenService.CreateAuthenticationToken(new Claim(ClaimTypes.Email, request.Email.ToLower()));
        else
            return null;
	}
}

Example registration controller

[Route("/api/account")]
public class RegisterController : ControllerBase
{
    private readonly ITokenService service;

    public AuthenticationController(ITokenService service)
    {
        this.service = service;
    }

    [HttpPost]
    [Route("register")]
    public void Register([FromBody] RegisterRequest request)
    {
        // DON'T ACTUALLY DO IT LIKE THIS
        // THIS IS ONLY AN EXAMPLE
		
        // Perform request validation checks
		
        bool exists = CheckUserDoesNotExistInYourPersistentStore(request.Email);
        if (exists)
            return false;
			
        // Create the password
        string salt = service.GenerateSalt();
        string hash = service.Hash(request.Password, salt);
	
        // Create the new user
        CreateNewUserInYourPersistentStore(new YourUser
        {
            Email = request.Email,
            Salt = salt,
            PasswordHash = hash
            // Other properties
        });
    }
}

Improvements

Raise them as an issue

Product 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.0.1 762 10/14/2018

Initial release.