Skip to content

Commit 0efd169

Browse files
committed
Updating the PR now that MSAL.NET's TokenCacheNotificationArgs has a member named IsApplicationTokenCache, brought by AzureAD/microsoft-authentication-library-for-dotnet#1448
cc: @bgavrilMS @jennyf19
1 parent dd03fc8 commit 0efd169

File tree

6 files changed

+21
-94
lines changed

6 files changed

+21
-94
lines changed

Microsoft.Identity.Web/Microsoft.Identity.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
5858
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="2.2.0" />
5959
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
60-
<PackageReference Include="Microsoft.Identity.Client" Version="4.3.1" />
60+
<PackageReference Include="Microsoft.Identity.Client" Version="4.5.1-internal20191022.1isappcache" />
6161
</ItemGroup>
6262
</Project>

Microsoft.Identity.Web/TokenCacheProviders/Distributed/DistributedTokenCacheAdapterExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static IServiceCollection AddDistributedAppTokenCache(
2929
this IServiceCollection services)
3030
{
3131
services.AddDistributedMemoryCache();
32-
services.AddSingleton<IMsalAppTokenCacheProvider, MsalAppDistributedTokenCacheProvider>();
32+
services.AddSingleton<IMsalTokenCacheProvider, MsalDistributedTokenCacheAdapter>();
3333
return services;
3434
}
3535

@@ -42,7 +42,7 @@ public static IServiceCollection AddDistributedUserTokenCache(
4242
{
4343
services.AddDistributedMemoryCache();
4444
services.AddHttpContextAccessor();
45-
services.AddSingleton<IMsalUserTokenCacheProvider, MsalPerUserDistributedTokenCacheProvider>();
45+
services.AddSingleton<IMsalTokenCacheProvider, MsalDistributedTokenCacheAdapter>();
4646
return services;
4747
}
4848
}

Microsoft.Identity.Web/TokenCacheProviders/Distributed/MsalAppDistributedTokenCacheProvider.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

Microsoft.Identity.Web/TokenCacheProviders/Distributed/MsalPerUserDistributedTokenCacheProvider.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

Microsoft.Identity.Web/TokenCacheProviders/MsalAbstractTokenCacheProvider.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,16 @@ public Task InitializeAsync(ITokenCache tokenCache, bool isAppTokenCache)
5959
/// <summary>
6060
/// Cache key
6161
/// </summary>
62-
private string CacheKey
62+
private string GetCacheKey(bool isAppTokenCache)
6363
{
64-
get
64+
if (isAppTokenCache)
6565
{
66-
if (_isAppTokenCache)
67-
{
68-
return $"{_azureAdOptions.Value.ClientId}_AppTokenCache";
69-
}
70-
else
71-
{
72-
return _httpContextAccessor.HttpContext.User.GetMsalAccountId();
73-
}
66+
return $"{_azureAdOptions.Value.ClientId}_AppTokenCache";
67+
}
68+
else
69+
{
70+
return _httpContextAccessor.HttpContext.User.GetMsalAccountId();
7471
}
75-
7672
}
7773

7874
/// <summary>
@@ -87,18 +83,21 @@ private async Task OnAfterAccessAsync(TokenCacheNotificationArgs args)
8783
// if the access operation resulted in a cache update
8884
if (args.HasStateChanged)
8985
{
90-
if (!string.IsNullOrWhiteSpace(CacheKey))
86+
string cacheKey = GetCacheKey(args.IsApplicationTokenCache);
87+
if (!string.IsNullOrWhiteSpace(cacheKey))
9188
{
92-
await WriteCacheBytesAsync(CacheKey, args.TokenCache.SerializeMsalV3()).ConfigureAwait(false);
89+
await WriteCacheBytesAsync(cacheKey, args.TokenCache.SerializeMsalV3()).ConfigureAwait(false);
9390
}
9491
}
9592
}
9693

9794
private async Task OnBeforeAccessAsync(TokenCacheNotificationArgs args)
9895
{
99-
if (!string.IsNullOrEmpty(CacheKey))
96+
string cacheKey = GetCacheKey(args.IsApplicationTokenCache);
97+
98+
if (!string.IsNullOrEmpty(cacheKey))
10099
{
101-
byte[] tokenCacheBytes = await ReadCacheBytesAsync(CacheKey).ConfigureAwait(false);
100+
byte[] tokenCacheBytes = await ReadCacheBytesAsync(cacheKey).ConfigureAwait(false);
102101
args.TokenCache.DeserializeMsalV3(tokenCacheBytes, shouldClearExistingCache: true);
103102
}
104103
}
@@ -111,7 +110,8 @@ protected virtual Task OnBeforeWriteAsync(TokenCacheNotificationArgs args)
111110

112111
public async Task ClearAsync()
113112
{
114-
await RemoveKeyAsync(CacheKey).ConfigureAwait(false);
113+
// This is here a user token cache
114+
await RemoveKeyAsync(GetCacheKey(false)).ConfigureAwait(false);
115115
}
116116

117117
protected abstract Task WriteCacheBytesAsync(string cacheKey, byte[] bytes);

Microsoft.Identity.Web/TokenCacheProviders/Session/SessionTokenCacheProviderExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static IServiceCollection AddSessionTokenCaches(this IServiceCollection s
7575
public static IServiceCollection AddSessionAppTokenCache(this IServiceCollection services)
7676
{
7777
services.AddHttpContextAccessor();
78-
services.AddScoped<IMsalAppTokenCacheProvider, MsalAppSessionTokenCacheProvider>();
78+
services.AddScoped<IMsalTokenCacheProvider, MsalSessionTokenCacheProvider>();
7979
return services;
8080
}
8181

@@ -101,7 +101,7 @@ public static IServiceCollection AddSessionPerUserTokenCache(this IServiceCollec
101101
services.AddSession(option =>
102102
{ option.Cookie.IsEssential = true; }
103103
);
104-
services.AddScoped<IMsalUserTokenCacheProvider, MsalPerUserSessionTokenCacheProvider>();
104+
services.AddScoped<IMsalTokenCacheProvider, MsalSessionTokenCacheProvider>();
105105
return services;
106106
}
107107
}

0 commit comments

Comments
 (0)