Buzzytom.Extensions.Authentication 1.0.1

Install-Package Buzzytom.Extensions.Authentication -Version 1.0.1
dotnet add package Buzzytom.Extensions.Authentication --version 1.0.1
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Buzzytom.Extensions.Authentication, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

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 460 10/14/2018

Initial release.