Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Westinm/compute cache key #2156

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ private async Task OnAfterAccessAsync(TokenCacheNotificationArgs args)

if (args.HasTokens)
{
await WriteCacheBytesAsync(args.SuggestedCacheKey, ProtectBytes(args.TokenCache.SerializeMsalV3()), cacheSerializerHints).ConfigureAwait(false);
await WriteCacheBytesAsync(GetSuggestedCacheKey(args), ProtectBytes(args.TokenCache.SerializeMsalV3()), cacheSerializerHints).ConfigureAwait(false);
}
else
{
// No token in the cache. we can remove the cache entry
await RemoveKeyAsync(args.SuggestedCacheKey, cacheSerializerHints).ConfigureAwait(false);
await RemoveKeyAsync(GetSuggestedCacheKey(args), cacheSerializerHints).ConfigureAwait(false);
}
}
}
Expand All @@ -106,9 +106,9 @@ private byte[] ProtectBytes(byte[] msalBytes)

private async Task OnBeforeAccessAsync(TokenCacheNotificationArgs args)
{
if (!string.IsNullOrEmpty(args.SuggestedCacheKey))
if (!string.IsNullOrEmpty(GetSuggestedCacheKey(args)))
{
byte[]? tokenCacheBytes = await ReadCacheBytesAsync(args.SuggestedCacheKey, CreateHintsFromArgs(args)).ConfigureAwait(false);
byte[]? tokenCacheBytes = await ReadCacheBytesAsync(GetSuggestedCacheKey(args), CreateHintsFromArgs(args)).ConfigureAwait(false);
if (tokenCacheBytes == null)
{
return;
Expand All @@ -124,7 +124,7 @@ private async Task OnBeforeAccessAsync(TokenCacheNotificationArgs args)
{
Logger.CacheDeserializationError(
_logger,
args.SuggestedCacheKey,
GetSuggestedCacheKey(args),
_protector != null,
exception.Message,
exception);
Expand Down Expand Up @@ -236,5 +236,15 @@ protected virtual Task RemoveKeyAsync(string cacheKey, CacheSerializerHints cach
{
return RemoveKeyAsync(cacheKey); // default implementation avoids a breaking change.
}

/// <summary>
/// Method to be overridden by concrete cache serializers to express the suggested key.
/// </summary>
/// <param name="args">Parameters used by MSAL call</param>
/// <returns>A string that contains the cache key suggested by MSAL.NET.</returns>
public virtual string GetSuggestedCacheKey(TokenCacheNotificationArgs args)
{
return args.SuggestedCacheKey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web.TokenCacheProviders;
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;

Expand Down Expand Up @@ -46,5 +47,10 @@ protected override Task WriteCacheBytesAsync(string cacheKey, byte[] bytes)
Count++;
return Task.CompletedTask;
}

public override string GetSuggestedCacheKey(TokenCacheNotificationArgs args)
{
return args.SuggestedCacheKey;
}
}
}