Skip to content

Commit bed6a6c

Browse files
committed
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.
1 parent 12e5980 commit bed6a6c

13 files changed

+12
-397
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

@@ -328,8 +325,8 @@ private IConfidentialClientApplication BuildConfidentialClientApplication()
328325
.Build();
329326

330327
// Initialize token cache providers
331-
_appTokenCacheProvider?.InitializeAsync(app.AppTokenCache);
332-
_userTokenCacheProvider?.InitializeAsync(app.UserTokenCache);
328+
_tokenCacheProvider?.InitializeAsync(app.AppTokenCache, true);
329+
_tokenCacheProvider?.InitializeAsync(app.UserTokenCache, false);
333330

334331
return app;
335332
}

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

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

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

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

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

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

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,59 +50,10 @@ public static IServiceCollection AddSessionTokenCaches(this IServiceCollection s
5050
});
5151
}
5252

53-
AddSessionAppTokenCache(services);
54-
AddSessionPerUserTokenCache(services);
53+
services.AddHttpContextAccessor();;
54+
services.AddScoped<IMsalTokenCacheProvider, MsalSessionTokenCacheProvider>();
5555

5656
return services;
5757
}
58-
59-
/// <summary>Adds the Http session based application token cache to the service collection.</summary>
60-
/// For this session cache to work effectively the aspnetcore session has to be configured properly.
61-
/// The latest guidance is provided at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state
62-
///
63-
/// // In the method - public void ConfigureServices(IServiceCollection services) in startup.cs, add the following
64-
/// services.AddSession(option =>
65-
/// {
66-
/// option.Cookie.IsEssential = true;
67-
/// });
68-
///
69-
/// In the method - public void Configure(IApplicationBuilder app, IHostingEnvironment env) in startup.cs, add the following
70-
///
71-
/// app.UseSession(); // Before UseMvc()
72-
///
73-
/// <param name="services">The services collection to add to.</param>
74-
/// <returns></returns>
75-
public static IServiceCollection AddSessionAppTokenCache(this IServiceCollection services)
76-
{
77-
services.AddHttpContextAccessor();
78-
services.AddScoped<IMsalAppTokenCacheProvider, MsalAppSessionTokenCacheProvider>();
79-
return services;
80-
}
81-
82-
/// <summary>Adds the http session based per user token cache to the service collection.</summary>
83-
/// For this session cache to work effectively the aspnetcore session has to be configured properly.
84-
/// The latest guidance is provided at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state
85-
///
86-
/// // In the method - public void ConfigureServices(IServiceCollection services) in startup.cs, add the following
87-
/// services.AddSession(option =>
88-
/// {
89-
/// option.Cookie.IsEssential = true;
90-
/// });
91-
///
92-
/// In the method - public void Configure(IApplicationBuilder app, IHostingEnvironment env) in startup.cs, add the following
93-
///
94-
/// app.UseSession(); // Before UseMvc()
95-
///
96-
/// <param name="services">The services collection to add to.</param>
97-
/// <returns></returns>
98-
public static IServiceCollection AddSessionPerUserTokenCache(this IServiceCollection services)
99-
{
100-
services.AddHttpContextAccessor();
101-
services.AddSession(option =>
102-
{ option.Cookie.IsEssential = true; }
103-
);
104-
services.AddScoped<IMsalUserTokenCacheProvider, MsalPerUserSessionTokenCacheProvider>();
105-
return services;
106-
}
10758
}
10859
}

0 commit comments

Comments
 (0)