69 lines
3.0 KiB
Markdown
69 lines
3.0 KiB
Markdown
|
# Lightweight library allowing to manage access token its lifetime with caching capability for services accounts with the oauth2 protocol
|
||
|
|
||
|
# Installing Nuget
|
||
|
|
||
|
> Install nuget package to your awesome project.
|
||
|
|
||
|
| Full Version | NuGet | NuGet Install |
|
||
|
|-----------------------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |----------------------------------------------------------------------------:|
|
||
|
| OpenHarbor.JwtTokenManager | <a href="https://www.nuget.org/packages/OpenHarbor.JwtTokenManager" target="_blank">[![NuGet](https://img.shields.io/nuget/v/OpenHarbor.JwtTokenManager.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager/)</a> | ```PM> Install-Package OpenHarbor.JwtTokenManager``` |
|
||
|
| OpenHarbor.JwtTokenManager.Abstractions | <a href="https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions" target="_blank">[![NuGet](https://img.shields.io/nuget/v/OpenHarbor.JwtTokenManager.Abstractions.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/OpenHarbor.JwtTokenManager.Abstractions/)</a> | ```PM> Install-Package OpenHarbor.JwtTokenManager.Abstractions``` |
|
||
|
|
||
|
# How to use
|
||
|
|
||
|
> appsettings.json
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"JwtTokenManager": {
|
||
|
"TokenEndpoint": "",
|
||
|
"ClientId": "",
|
||
|
"ClientSecret": "",
|
||
|
"Scopes": []
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
> Program.cs
|
||
|
|
||
|
```csharp
|
||
|
builder.Services.AddHttpClient();
|
||
|
builder.Services.AddMemoryCache(); // use the IMemoryCache provider you want
|
||
|
|
||
|
builder.Services.AddJwtTokenManager(builder.Configuration, "JwtTokenManager");
|
||
|
```
|
||
|
|
||
|
> Program.cs with deeper configurations
|
||
|
|
||
|
```csharp
|
||
|
builder.Services.AddHttpClient();
|
||
|
|
||
|
builder.Services.AddJwtTokenManager(builder.Configuration, "JwtTokenManager", options =>
|
||
|
{
|
||
|
options.Cache(cacheOptions =>
|
||
|
{
|
||
|
cacheOptions.ExpirationOffset = 30; // 15 by default
|
||
|
},
|
||
|
// optional to configure your own IMemoryCache provider for the token management
|
||
|
provider => provider.GetRequiredService<IMemoryCache>());
|
||
|
|
||
|
options.Configuration(overrideConfiguration =>
|
||
|
{
|
||
|
overrideConfiguration.Scopes = ["offline_access"];
|
||
|
});
|
||
|
});
|
||
|
```
|
||
|
|
||
|
> Use the JwtTokenManager
|
||
|
|
||
|
```csharp
|
||
|
public class MyService(HttpClient httpClient, IJwtTokenManagerService jwtTokenManagerService)
|
||
|
{
|
||
|
public async Task DoActionAsync(CancellationToken cancellationToken) {
|
||
|
|
||
|
var tokenResult = await jwtTokenManagerService.GetTokenAsync(cancellationToken);
|
||
|
httpClient.SetJwtAccessToken(tokenResult);
|
||
|
...
|
||
|
}
|
||
|
}
|
||
|
```
|