AspNetCoreAuthenticationTokenExtension 2.0.0

dotnet add package AspNetCoreAuthenticationTokenExtension --version 2.0.0
                    
NuGet\Install-Package AspNetCoreAuthenticationTokenExtension -Version 2.0.0
                    
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="AspNetCoreAuthenticationTokenExtension" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AspNetCoreAuthenticationTokenExtension" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AspNetCoreAuthenticationTokenExtension" />
                    
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 AspNetCoreAuthenticationTokenExtension --version 2.0.0
                    
#r "nuget: AspNetCoreAuthenticationTokenExtension, 2.0.0"
                    
#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 AspNetCoreAuthenticationTokenExtension@2.0.0
                    
#: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=AspNetCoreAuthenticationTokenExtension&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AspNetCoreAuthenticationTokenExtension&version=2.0.0
                    
Install as a Cake Tool

AspNetCoreAuthenticationTokenExtension

A extension to use token authentication

Installation

Install via NuGet Package Manager:

dotnet add package AspNetCoreAuthenticationTokenExtension

Usage

add the following using statement in your program.cs

using AspNetCoreAuthenticationTokenExtension

then use the TokenAuthentication in following way

builder.Services.AddAuthentication()
.AddTokenAuthentication(YourScheme, YourSchemeDisplayName, ActionToConfigureOptions);

do not forget to add

app.UseAuthentication()

During Login. First create your claimsIdentity with the same schemeName that is used to register the TokenAuthentication. E.g.

// all the checks for password checking are done before
await HttpContext.SignInAsync(YourScheme || TokenAuthenticationDefaults.SchemeName, Your User)

During logout. Use the SignOutAsync method with your scheme name.

  • Remember that if your not using a TokenStore. And after signout still someone sends the older token. They will be considered valid till the time they are expired.
await HttpContext.SignoutAsync(YourScheme || TokenAuthenticationDefaults.SchemeName)
  • Provide Secret Key

    • Secret Key is used to protect data as well as to validate the authenticity of the token.
    • if you don't provide one then a random key will be generated which will be different every time application starts.
  • Provide TokenStore

    • It is used to store tokens
    • It is recommended to consider using any store as per your use case
    • If no store is provided and you are not using cookies then even after signout your token is still valid till the time it is expired
    • If you are not using any store then it is recommended to provide expirations value

How Tos ?

  • For this you have to create an implementation for ITokenRetriever
  • After that you have to provide your implementation to config options e.g.
builder.Services.AddAuthentication()
.AddTokenAuthentication(YourScheme, YourSchemeDisplayName, (opts) => {
    opts.TokenRetriver = 'your implementation goes here'
});

How do i use DI in my implementations

  • For this you can take benifit of
IPostConfigureOptions<TokenAuthenticationOptions>
  • You have to implement this and inside the implementation there is a function
public void PostConfigure(string name, TokenAuthenticationOptions options)

you can update the options here

  • Inside the implementation you can use DI like
class MyCustomImplementation : IPostConfigureOptions<TokenAuthenticationOptions>
{
    private IMyService _myTokenRetriever;

    public MyCustomImplementation(ITokenRetriever myTokenRetriever)
    {
       _myTokenRetriever = myTokenRetriever;
    }

    public void PostConfigure(string name, TokenAuthenticationOptions options)
    {
        options.TokenRetriver = _myTokenRetriever;
    }
}

How do i use different encryption algo or different type of tokens

  • For this you have to implement ITokenDataFormat according to your requirement
  • And provide this to config

Some Bonus Tips

  • All the interfaces are in AspNetCoreAuthenticationTokenExtension.Interface namespace
  • As o now JWT tokens are used.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
2.0.0 155 6/24/2024

Authenticate user using tokens which can be stored based on provided config and can be provided in response in either way like header or cookie.