AspNetCoreAuthenticationTokenExtension 2.0.0
dotnet add package AspNetCoreAuthenticationTokenExtension --version 2.0.0
NuGet\Install-Package AspNetCoreAuthenticationTokenExtension -Version 2.0.0
<PackageReference Include="AspNetCoreAuthenticationTokenExtension" Version="2.0.0" />
<PackageVersion Include="AspNetCoreAuthenticationTokenExtension" Version="2.0.0" />
<PackageReference Include="AspNetCoreAuthenticationTokenExtension" />
paket add AspNetCoreAuthenticationTokenExtension --version 2.0.0
#r "nuget: AspNetCoreAuthenticationTokenExtension, 2.0.0"
#:package AspNetCoreAuthenticationTokenExtension@2.0.0
#addin nuget:?package=AspNetCoreAuthenticationTokenExtension&version=2.0.0
#tool nuget:?package=AspNetCoreAuthenticationTokenExtension&version=2.0.0
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)
Recommended Config That Should be considered at minimal
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 ?
How do i read token from cookie & write back to cookie
- 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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Authentication (>= 2.2.0)
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.