-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a new DistributedTokenCacheProvider (#173)
* Adding a new DistributedTokenCacheProvider This enables to decouple the serialization itself (done by a.NET Core IDistrributedCache implementation), from the token cache logic (done by the DistributedTokenCacheProvider) See https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.2#distributed-memory-cache. ```CSharp // or use a distributed Token Cache by adding .AddDistributedTokenCaches(); // and then choose your implementation. // See https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.2#distributed-memory-cache // For instance the distributed in memory cache (not cleaned when you stop the app) services.AddDistributedMemoryCache() // Or a Redis cache services.AddStackExchangeRedisCache(options => { options.Configuration = "localhost"; options.InstanceName = "SampleInstance"; }); // Or even a SQL Server token cache services.AddDistributedSqlServerCache(options => { options.ConnectionString = _config["DistCache_ConnectionString"]; options.SchemaName = "dbo"; options.TableName = "TestCache"; }); ``` * processing PR feedback * Add more comments * Improving the identation * Updating the README.md with new pictures, and details about the Distributed token caches * updating the diagrams * Renaming DistributedTokenCacheProvider to DistributedTokenCacheAdapter as this is an adapter in this particular case cc: @bgavrilMS
- Loading branch information
Showing
8 changed files
with
289 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...oft.Identity.Web/TokenCacheProviders/Distributed/DistributedTokenCacheAdapterExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.Identity.Web.TokenCacheProviders.Distributed | ||
{ | ||
/// <summary> | ||
/// Extension class used to add an in-memory token cache serializer to MSAL | ||
/// </summary> | ||
public static class DistributedTokenCacheAdapterExtension | ||
{ | ||
/// <summary>Adds both the app and per-user in-memory token caches.</summary> | ||
/// <param name="services">The services collection to add to.</param> | ||
/// <param name="cacheOptions">The MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddDistributedTokenCaches( | ||
this IServiceCollection services) | ||
{ | ||
AddDistributedAppTokenCache(services); | ||
AddDistributedUserTokenCache(services); | ||
return services; | ||
} | ||
|
||
/// <summary>Adds the in-memory based application token cache to the service collection.</summary> | ||
/// <param name="services">The services collection to add to.</param> | ||
/// <param name="cacheOptions">The MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param> | ||
public static IServiceCollection AddDistributedAppTokenCache( | ||
this IServiceCollection services) | ||
{ | ||
services.AddDistributedMemoryCache(); | ||
services.AddSingleton<IMsalAppTokenCacheProvider, MsalAppDistributedTokenCacheProvider>(); | ||
return services; | ||
} | ||
|
||
/// <summary>Adds the in-memory based per user token cache to the service collection.</summary> | ||
/// <param name="services">The services collection to add to.</param> | ||
/// <param name="cacheOptions">The MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddDistributedUserTokenCache( | ||
this IServiceCollection services) | ||
{ | ||
services.AddDistributedMemoryCache(); | ||
services.AddHttpContextAccessor(); | ||
services.AddSingleton<IMsalUserTokenCacheProvider, MsalPerUserDistributedTokenCacheProvider>(); | ||
return services; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...soft.Identity.Web/TokenCacheProviders/Distributed/MsalAppDistributedTokenCacheProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication.AzureAD.UI; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Identity.Client; | ||
|
||
namespace Microsoft.Identity.Web.TokenCacheProviders.Distributed | ||
{ | ||
/// <summary> | ||
/// An implementation of token cache for Confidential clients backed by MemoryCache. | ||
/// MemoryCache is useful in Api scenarios where there is no HttpContext to cache data. | ||
/// </summary> | ||
/// <seealso cref="https://aka.ms/msal-net-token-cache-serialization"/> | ||
public class MsalAppDistributedTokenCacheProvider : MsalDistributedTokenCacheAdapter, IMsalAppTokenCacheProvider | ||
{ | ||
public MsalAppDistributedTokenCacheProvider(IOptions<AzureADOptions> azureAdOptions, | ||
IHttpContextAccessor httpContextAccessor, | ||
IDistributedCache memoryCache, | ||
IOptions<DistributedCacheEntryOptions> cacheOptions) : | ||
base(azureAdOptions, httpContextAccessor, memoryCache, cacheOptions) | ||
{ | ||
|
||
} | ||
|
||
public async Task InitializeAsync(ITokenCache tokenCache) | ||
{ | ||
await InitializeAsync(tokenCache, true).ConfigureAwait(false); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
Microsoft.Identity.Web/TokenCacheProviders/Distributed/MsalDistributedTokenCacheAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication.AzureAD.UI; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Identity.Web.TokenCacheProviders.Distributed | ||
{ | ||
/// <summary> | ||
/// An implementation of token cache for both Confidential and Public clients backed by MemoryCache. | ||
/// </summary> | ||
/// <seealso cref="https://aka.ms/msal-net-token-cache-serialization"/> | ||
public class MsalDistributedTokenCacheAdapter : MsalAbstractTokenCacheProvider | ||
{ | ||
/// <summary> | ||
/// .NET Core Memory cache | ||
/// </summary> | ||
private readonly IDistributedCache _distributedCache; | ||
|
||
/// <summary> | ||
/// Msal memory token cache options | ||
/// </summary> | ||
private readonly DistributedCacheEntryOptions _cacheOptions; | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="azureAdOptions"></param> | ||
/// <param name="httpContextAccessor"></param> | ||
/// <param name="memoryCache"></param> | ||
/// <param name="cacheOptions"></param> | ||
public MsalDistributedTokenCacheAdapter(IOptions<AzureADOptions> azureAdOptions, | ||
IHttpContextAccessor httpContextAccessor, | ||
IDistributedCache memoryCache, | ||
IOptions<DistributedCacheEntryOptions> cacheOptions) : | ||
base(azureAdOptions, httpContextAccessor) | ||
{ | ||
_distributedCache = memoryCache; | ||
_cacheOptions = cacheOptions.Value; | ||
} | ||
|
||
protected override async Task RemoveKeyAsync(string cacheKey) | ||
{ | ||
await _distributedCache.RemoveAsync(cacheKey).ConfigureAwait(false); | ||
} | ||
|
||
protected override async Task<byte[]> ReadCacheBytesAsync(string cacheKey) | ||
{ | ||
return await _distributedCache.GetAsync(cacheKey).ConfigureAwait(false); | ||
} | ||
|
||
protected override async Task WriteCacheBytesAsync(string cacheKey, byte[] bytes) | ||
{ | ||
await _distributedCache.SetAsync(cacheKey, bytes, _cacheOptions).ConfigureAwait(false) ; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
....Identity.Web/TokenCacheProviders/Distributed/MsalPerUserDistributedTokenCacheProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.AspNetCore.Authentication.AzureAD.UI; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Identity.Client; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
|
||
|
||
namespace Microsoft.Identity.Web.TokenCacheProviders.Distributed | ||
{ | ||
/// <summary> | ||
/// An implementation of token cache for both Confidential and Public clients backed by MemoryCache. | ||
/// MemoryCache is useful in Api scenarios where there is no HttpContext.Session to cache data. | ||
/// </summary> | ||
/// <seealso cref="https://aka.ms/msal-net-token-cache-serialization"/> | ||
public class MsalPerUserDistributedTokenCacheProvider : MsalDistributedTokenCacheAdapter, IMsalUserTokenCacheProvider | ||
{ | ||
public MsalPerUserDistributedTokenCacheProvider(IOptions<AzureADOptions> azureAdOptions, | ||
IHttpContextAccessor httpContextAccessor, | ||
IDistributedCache memoryCache, | ||
IOptions<DistributedCacheEntryOptions> cacheOptions) : | ||
base(azureAdOptions, httpContextAccessor, memoryCache, cacheOptions) | ||
{ | ||
|
||
} | ||
|
||
public async Task InitializeAsync(ITokenCache tokenCache) | ||
{ | ||
await InitializeAsync(tokenCache, false).ConfigureAwait(false); | ||
} | ||
} | ||
} |