PhilipDaubmeier.TokenStore 1.6.0

dotnet add package PhilipDaubmeier.TokenStore --version 1.6.0                
NuGet\Install-Package PhilipDaubmeier.TokenStore -Version 1.6.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="PhilipDaubmeier.TokenStore" Version="1.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PhilipDaubmeier.TokenStore --version 1.6.0                
#r "nuget: PhilipDaubmeier.TokenStore, 1.6.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.
// Install PhilipDaubmeier.TokenStore as a Cake Addin
#addin nuget:?package=PhilipDaubmeier.TokenStore&version=1.6.0

// Install PhilipDaubmeier.TokenStore as a Cake Tool
#tool nuget:?package=PhilipDaubmeier.TokenStore&version=1.6.0                

NuGet Build status

TokenStore

TokenStore is a .NET Core class library providing a means to store and retrieve OAuth access and refresh tokens to/from a configurable entity framework database.

NuGet

PM> Install-Package PhilipDaubmeier.TokenStore

Usage

If you already have a database context, include the interface ITokenStoreDbContext, or create a new DbContext class that derives from it:

public class MyDbContext : DbContext, ITokenStoreDbContext
{
    public DbSet<AuthData> AuthDataSet { get; set; }

    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    { }
}

Add a config section in your appsettings.json and define how the key in the database should be named for your class (can be any string you like):

"TokenStoreConfig": {
  "ClassNameMapping": {
    "MyClassUsingTheTokenStore": "my_class_db_key"
  }
}

Set up the TokenStore for dependency injection in your Startup.cs, configuring the entity framework database context:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
    services.ConfigureTokenStore(configuration.GetSection("TokenStoreConfig"));
    services.AddTokenStoreDbContext<MyDbContext>(opt => opt.UseSqlServer("<my_connection_str>"));

    services.AddTokenStore<MyClassUsingTheTokenStore>();
}

You can then use the TokenStore by getting it dependency injected into a class where you need it:

public class MyClassUsingTheTokenStore
{
    private readonly TokenStore<MyClassUsingTheTokenStore> tokenStore;

    public MyClassUsingTheTokenStore(TokenStore<MyClassUsingTheTokenStore> tokenStore)
    {
        this.tokenStore = tokenStore;
    }

    public async Task DemoStoreSomeToken()
    {
        await tokenStore.UpdateToken("demo access token", DateTime.Now, "demo refresh token");
    }

    public void DemoLoadTheToken()
    {
        if (tokenStore.IsAccessTokenValid())
            Console.WriteLine($"Access Token is valid: ${tokenStore.AccessToken}");

        Console.WriteLine($"AT invalid, refresh token: ${tokenStore.RefreshToken}");
    }
}

You can also have a look at classes where TokenStore is used in GraphIoT, like PersistingDigitalstromAuth.

Platform Support

TokenStore is targeted for .NET 9.0 or higher.

License

The MIT License (MIT)

Copyright (c) 2019-2024 Philip Daubmeier

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Compatible and additional computed target framework versions.
.NET net9.0 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.6.0 73 12/2/2024
1.4.0 467 6/27/2021
1.3.1 451 4/17/2020
1.3.0 418 4/14/2020
1.2.0 474 3/29/2020
1.1.0 496 10/16/2019
1.0.0 562 10/8/2019

1.6.0 - upgraded to target framework net9.0
1.5.0 - upgraded to target framework net8.0
1.4.0 - upgraded to target framework net5.0
1.3.1 - fixed multi TFM issues
1.3.0 - widened range of supported platforms by targeting netstandard2.1
1.2.0 - upgraded to target framework netcoreapp3.1
1.1.0 - migrated to netcoreapp3.0
1.0.0 - Initial version with publishing version 1.0 of GraphIoT