GenericTokenExtractor 1.0.0

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

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)
  • IGenericTokenExtractor for 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 Authorization header 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 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
1.0.0 276 8/27/2025