ProtectorTokenAuth 1.0.1

dotnet add package ProtectorTokenAuth --version 1.0.1
                    
NuGet\Install-Package ProtectorTokenAuth -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="ProtectorTokenAuth" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ProtectorTokenAuth" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ProtectorTokenAuth" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ProtectorTokenAuth --version 1.0.1
                    
#r "nuget: ProtectorTokenAuth, 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.
#:package ProtectorTokenAuth@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ProtectorTokenAuth&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ProtectorTokenAuth&version=1.0.1
                    
Install as a Cake Tool

Protector Auth Token

Simple token authentication for .NET Core API and MVC projects. It looks for an access token in the request header under the "ProtectorToken" key and verifies its validity when accessing a controller or an action tagged with the [Protect] attribute.

This project uses Microsoft's Data Protection library (https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-6.0) to secure the access token.

Usage:

  1. Add the ProtectorTokenAuth (https://www.nuget.org/packages/ProtectorTokenAuth/) Nuget package to your .NET Core API or MVC project.
  2. Enable it in your project's Startup.cs:
public void ConfigureServices(IServiceCollection services) {
  services.AddProtectorAuth();
}

NOTE: It is recommended to use the AddProtectorAuthWithOptions() method and specify your own unique provider name so that tokens issued from other systems won't be valid on yours.

  1. Inject the IAuthService authentication service into any controllers or services you wish to create or validate access tokens and hash passwords:
public class MyController : ControllerBase 
{
  public MyController(IAuthService authService) 
  {
    this.authService = authService;
  }
}
  1. Issue a token on successful login like so:
public string YourLoginMethod()
{
  // ... login functionality here
  
  return authService.CreateAccessToken(new TokenPayload
  {
    Username = username,
    UserRoles = new string[] { roleName },
    CustomData = new Dictionary<string,string>()
  });
}
  1. Protect your controllers or actions using the [Protect] attribute. You can also specify which roles the attribute should allow.
[Protect]
public StatusMessageVM SomeMethod() {
  // ...
}

[Protect("Admin")]
public StatusMessageVM AdminOnlyMethod() {
  // ...
}


[Protect("Admin", "Moderator")]
public StatusMessageVM AdminAndModeratorOnlyMethod() {
  // ...
}
  1. You can access a user's name, roles and custom data everywhere the IAuthService is injected by looking at authService.ActiveUser, authService.ActiveUserRoles and authService.ActiveUserData

  2. You can use the HashPassword of the AuthService to hash passwords before storing them to your database. It has an optional salt parameter for further security.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
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 422 5/16/2022
1.0.0 365 5/16/2022

Exposed user custom data through AuthService.