GenericTokenExtractor 1.0.0
dotnet add package GenericTokenExtractor --version 1.0.0
NuGet\Install-Package GenericTokenExtractor -Version 1.0.0
<PackageReference Include="GenericTokenExtractor" Version="1.0.0" />
<PackageVersion Include="GenericTokenExtractor" Version="1.0.0" />
<PackageReference Include="GenericTokenExtractor" />
paket add GenericTokenExtractor --version 1.0.0
#r "nuget: GenericTokenExtractor, 1.0.0"
#:package GenericTokenExtractor@1.0.0
#addin nuget:?package=GenericTokenExtractor&version=1.0.0
#tool nuget:?package=GenericTokenExtractor&version=1.0.0
GenericTokenExtractor
A lightweight .NET package that provides an easy way to access tokens (such as JWTs) from the current HttpContext in ASP.NET Core applications.
It is designed to be simple, dependency-injected, and ready to use.
π¦ Installation
You can install the package via NuGet:
dotnet add package GenericTokenExtractor --version <version>
Or via the Visual Studio NuGet Package Manager.
π Getting Started
1. Register the GenericTokenExtractor in your Startup / Program.cs
using Microsoft.Extensions.DependencyInjection;
using GenericTokenExtractor;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add GenericTokenExtractor service
services.GenericTokenExtractor();
// Other services
}
}
This extension method will register:
IHttpContextAccessor(required to access the current HttpContext)IGenericTokenExtractorfor resolving tokens
2. Inject and Use the IGenericTokenExtractor
You can now inject IGenericTokenExtractor into any service or controller.
using Microsoft.AspNetCore.Mvc;
using GenericTokenExtractor;
[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
private readonly IGenericTokenExtractor _tokenProvider;
public SampleController(IGenericTokenExtractor tokenProvider)
{
_tokenProvider = tokenProvider;
}
[HttpGet("user-token")]
public IActionResult GetUserToken()
{
var token = _tokenProvider.GetToken();
if (string.IsNullOrEmpty(token))
return Unauthorized("No token found");
return Ok(new { Token = token });
}
}
π§ Features
- β
Provides a clean abstraction
IGenericTokenExtractor - β
Automatically retrieves tokens from
Authorizationheader in HttpContext - β Works with JWT Bearer tokens and custom headers
- β Easy to integrate via Dependency Injection
- β Minimal setup required
β FAQ (For Consumers)
Q: Do I need to manually add IHttpContextAccessor?
No, AddGenericTokenExtractor() automatically registers it for you.
Q: Where does the GenericTokenExtractor fetch the token from?
By default, it looks for the Authorization header in the current HttpContext.
Q: What if thereβs no token present?
The GetToken() method will return null or an empty string. You can handle it as per your appβs requirements.
Q: Can I use it outside of ASP.NET Core apps?
Not directly β it depends on HttpContext. However, you can adapt it for background services that run within the ASP.NET Core host.
Q: Does it validate or decode the token?
No, this package only retrieves the token. Validation/decoding should be handled separately (e.g., using Microsoft Identity packages or JWT libraries).
π Example Usage in a Service
public class MyBusinessService
{
private readonly IGenericTokenExtractor _tokenProvider;
public MyBusinessService(IGenericTokenExtractor tokenProvider)
{
_tokenProvider = tokenProvider;
}
public void DoWork()
{
var token = _tokenProvider.GetToken();
Console.WriteLine($"Token: {token}");
}
}
π Versioning
This package follows semantic versioning.
Breaking changes will be introduced in major versions (1.x β 2.x).
π€ Contributing
Contributions are welcome!
Please fork the repository and create a pull request.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
| 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.Http (>= 2.3.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Options (>= 9.0.8)
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.0 | 276 | 8/27/2025 |