- OAuth 2.0 Client Library for .NET
- Supports >= .NET Core 1.1 and .NET Framework 4.5.2
- Provides implementions which automatically cache authentication server responses
- Supports the following grant types
- password
- client_credentials
- refresh_token
This repository contains the source code for the TokenClient
and CachingTokenClient
which is an OAuth2.0 client for .NET. The caching version automatically caches the authentication server response in memory on the server.
If the server response includes a value for expires_in
, the response is cached up until this value. If the server does not include this value, it is cached for 24 hours, though this can be overridden using the DefaultCacheExpiry
option.
Note: If you are using a refresh token, you should store this separately, and use it to get a new token once your access token expires.
- Requires >= .NET Core 1.1
Please use the .NET Standard package for .NET Core applications.
- Install the
InfoTrack.OAuth.Caching.DotNetCore
NuGet package.
PM> Install-Package InfoTrack.OAuth.Caching.DotNetCore
- Register the .NET Core Memory Cache in your application during startup when configuring your services. You can also optionally register the
CachingTokenClient
for dependency injection.
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddSingleton<ITokenClient, CachingTokenClient>();
}
- Whenever you need a token, inject an ITokenClient, and call a token method. It will return the cached token unless it has expired, at which point it will grab a new one.
var tokenResponse = await tokenClient.ClientCredentialsGrantAsync(
new Uri("https://authenticate.me/connect/token"),
"client_id",
"client_secret"
);
You may register the CachingTokenClient
with any dependency lifecycle as it is fully thread safe.
- Profit 🤑
- Requires >= .NET Framework 4.5.2
This package contains a .NET Framework implementation of the CachingTokenClient
. Internally this uses System.Runtime.Caching.MemoryCache
.
- Install the
InfoTrack.OAuth.Caching.DotNetFramework
NuGet package.
PM> Install-Package InfoTrack.OAuth.Caching.DotNetFramework
- Optionally register the
CachingTokenClient
for dependency injection.
// Ninject example
_kernel.Bind<ITokenClient>().To<CachingTokenClient>();
- Whenever you need a token, inject an ITokenClient, and call a token method. It will return the cached token unless it has expired, at which point it will grab a new one.
var tokenResponse = await tokenClient.ClientCredentialsGrantAsync(
new Uri("https://authenticate.me/connect/token"),
"client_id",
"client_secret"
);
You may register the CachingTokenClient
with any dependency lifecycle as it is fully thread safe.
- Requires >= .NET Standard 2.0
This package contains a .NET Standard implementation of the CachingTokenClient
. Internally this uses System.Runtime.Caching.MemoryCache
.
- Install the
InfoTrack.OAuth.Caching.DotNetStandard
NuGet package.
PM> Install-Package InfoTrack.OAuth.Caching.DotNetStandard
- Optionally register the
CachingTokenClient
for dependency injection.
// Ninject example
_kernel.Bind<ITokenClient>().To<CachingTokenClient>();
- Whenever you need a token, inject an ITokenClient, and call a token method. It will return the cached token unless it has expired, at which point it will grab a new one.
var tokenResponse = await tokenClient.ClientCredentialsGrantAsync(
new Uri("https://authenticate.me/connect/token"),
"client_id",
"client_secret"
);
You may register the CachingTokenClient
with any dependency lifecycle as it is fully thread safe.
If all you need is a basic token client with very few dependencies, you can use the base package on its own.
PM> Install-Package InfoTrack.OAuth
There are constructors for the CachingTokenClient
which allow you to pass a ClientOptions
object.
Property | Default | Remarks |
---|---|---|
DefaultCacheExpiry | 86400 | Time in seconds to cache the response if it does not contain a value for expires_in |