Skip to content

Commit feb0a2e

Browse files
authored
Attempt to simplify the token cache providers (#172)
* Attempt to simplify the token cache providers DO NOT MERGE Ideally we'd want the implementation of the token cache providers to be simplified. However this is not yet possible because the MSAL Abstract token cache provider contains a boolean telling if the token cache is an app or user token cache (so that the key can be computed). Ideally we'd want the ITokenCache to tell the application if it's an app token cache or a user token cache. * 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 * Further simplifying the token cache implementations
1 parent a685819 commit feb0a2e

18 files changed

+49
-451
lines changed

2-WebApp-graph-user/2-2-TokenCache/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public void ConfigureServices(IServiceCollection services)
5151
// and chosen token cache implementation
5252
services.AddMicrosoftIdentityPlatformAuthentication(Configuration)
5353
.AddMsal(Configuration, new string[] { Constants.ScopeUserRead })
54-
.AddSqlAppTokenCache(msalSqlTokenCacheOptions)
55-
.AddSqlPerUserTokenCache(msalSqlTokenCacheOptions);
54+
.AddSqlTokenCaches(msalSqlTokenCacheOptions);
5655

5756

5857
// Add Graph

Microsoft.Identity.Web/TokenAcquisition.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public class TokenAcquisition : ITokenAcquisition
2929
private readonly AzureADOptions _azureAdOptions;
3030
private readonly ConfidentialClientApplicationOptions _applicationOptions;
3131

32-
private readonly IMsalAppTokenCacheProvider _appTokenCacheProvider;
33-
private readonly IMsalUserTokenCacheProvider _userTokenCacheProvider;
32+
private readonly IMsalTokenCacheProvider _tokenCacheProvider;
3433

3534
private IConfidentialClientApplication application;
3635
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -42,20 +41,18 @@ public class TokenAcquisition : ITokenAcquisition
4241
/// This constructor is called by ASP.NET Core dependency injection
4342
/// </summary>
4443
/// <param name="configuration"></param>
45-
/// <param name="appTokenCacheProvider">The App token cache provider</param>
44+
/// <param name="tokenCacheProvider">The App token cache provider</param>
4645
/// <param name="userTokenCacheProvider">The User token cache provider</param>
4746
public TokenAcquisition(
48-
IMsalAppTokenCacheProvider appTokenCacheProvider,
49-
IMsalUserTokenCacheProvider userTokenCacheProvider,
47+
IMsalTokenCacheProvider tokenCacheProvider,
5048
IHttpContextAccessor httpContextAccessor,
5149
IOptions<AzureADOptions> azureAdOptions,
5250
IOptions<ConfidentialClientApplicationOptions> applicationOptions)
5351
{
5452
_httpContextAccessor = httpContextAccessor;
5553
_azureAdOptions = azureAdOptions.Value;
5654
_applicationOptions = applicationOptions.Value;
57-
_appTokenCacheProvider = appTokenCacheProvider;
58-
_userTokenCacheProvider = userTokenCacheProvider;
55+
_tokenCacheProvider = tokenCacheProvider;
5956
}
6057

6158
/// <summary>
@@ -283,7 +280,7 @@ public async Task RemoveAccountAsync(RedirectContext context)
283280
if (account != null)
284281
{
285282
await app.RemoveAsync(account).ConfigureAwait(false);
286-
_userTokenCacheProvider?.ClearAsync().ConfigureAwait(false);
283+
_tokenCacheProvider?.ClearAsync().ConfigureAwait(false);
287284
}
288285
}
289286

@@ -326,8 +323,8 @@ private IConfidentialClientApplication BuildConfidentialClientApplication()
326323
.Build();
327324

328325
// Initialize token cache providers
329-
_appTokenCacheProvider?.InitializeAsync(app.AppTokenCache);
330-
_userTokenCacheProvider?.InitializeAsync(app.UserTokenCache);
326+
_tokenCacheProvider?.InitializeAsync(app.AppTokenCache);
327+
_tokenCacheProvider?.InitializeAsync(app.UserTokenCache);
331328

332329
return app;
333330
}

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/IMSALUserTokenCacheProvider.cs

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

Microsoft.Identity.Web/TokenCacheProviders/IMsalAppTokenCacheProvider.cs

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

Microsoft.Identity.Web/TokenCacheProviders/IMsalTokenCacheProvider .cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IMsalTokenCacheProvider
1818
/// <param name="isAppTokenCache">Is the token cache an App token cache or
1919
/// a user token cache</param>
2020
/// <returns></returns>
21-
Task InitializeAsync(ITokenCache tokenCache, bool isAppTokenCache);
21+
Task InitializeAsync(ITokenCache tokenCache);
2222

2323
/// <summary>
2424
/// Clear the cache

Microsoft.Identity.Web/TokenCacheProviders/InMemory/InMemoryTokenCacheProviderExtension.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,10 @@ public static class InMemoryTokenCacheProviderExtension
1616
/// <returns></returns>
1717
public static IServiceCollection AddInMemoryTokenCaches(
1818
this IServiceCollection services)
19-
{
20-
AddInMemoryAppTokenCache(services);
21-
AddInMemoryPerUserTokenCache(services);
22-
return services;
23-
}
24-
25-
/// <summary>Adds the in-memory based application token cache to the service collection.</summary>
26-
/// <param name="services">The services collection to add to.</param>
27-
/// <param name="cacheOptions">The MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param>
28-
public static IServiceCollection AddInMemoryAppTokenCache(
29-
this IServiceCollection services)
30-
{
31-
services.AddMemoryCache();
32-
services.AddSingleton<IMsalAppTokenCacheProvider, MsalAppMemoryTokenCacheProvider>();
33-
return services;
34-
}
35-
36-
/// <summary>Adds the in-memory based per user token cache to the service collection.</summary>
37-
/// <param name="services">The services collection to add to.</param>
38-
/// <param name="cacheOptions">The MSALMemoryTokenCacheOptions allows the caller to set the token cache expiration</param>
39-
/// <returns></returns>
40-
public static IServiceCollection AddInMemoryPerUserTokenCache(
41-
this IServiceCollection services)
4219
{
4320
services.AddMemoryCache();
4421
services.AddHttpContextAccessor();
45-
services.AddSingleton<IMsalUserTokenCacheProvider, MsalPerUserMemoryTokenCacheProvider>();
22+
services.AddSingleton<IMsalTokenCacheProvider, MsalMemoryTokenCacheProvider>();
4623
return services;
4724
}
4825
}

Microsoft.Identity.Web/TokenCacheProviders/InMemory/MsalAppMemoryTokenCacheProvider.cs

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

Microsoft.Identity.Web/TokenCacheProviders/InMemory/MsalPerUserMemoryTokenCacheProvider.cs

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

0 commit comments

Comments
 (0)