Skip to content

Attempt to simplify the token cache providers #172

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

Merged
merged 5 commits into from
Nov 4, 2019
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
3 changes: 1 addition & 2 deletions 2-WebApp-graph-user/2-2-TokenCache/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public void ConfigureServices(IServiceCollection services)
// and chosen token cache implementation
services.AddMicrosoftIdentityPlatformAuthentication(Configuration)
.AddMsal(Configuration, new string[] { Constants.ScopeUserRead })
.AddSqlAppTokenCache(msalSqlTokenCacheOptions)
.AddSqlPerUserTokenCache(msalSqlTokenCacheOptions);
.AddSqlTokenCaches(msalSqlTokenCacheOptions);


// Add Graph
Expand Down
17 changes: 7 additions & 10 deletions Microsoft.Identity.Web/TokenAcquisition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class TokenAcquisition : ITokenAcquisition
private readonly AzureADOptions _azureAdOptions;
private readonly ConfidentialClientApplicationOptions _applicationOptions;

private readonly IMsalAppTokenCacheProvider _appTokenCacheProvider;
private readonly IMsalUserTokenCacheProvider _userTokenCacheProvider;
private readonly IMsalTokenCacheProvider _tokenCacheProvider;

private IConfidentialClientApplication application;
private readonly IHttpContextAccessor _httpContextAccessor;
Expand All @@ -42,20 +41,18 @@ public class TokenAcquisition : ITokenAcquisition
/// This constructor is called by ASP.NET Core dependency injection
/// </summary>
/// <param name="configuration"></param>
/// <param name="appTokenCacheProvider">The App token cache provider</param>
/// <param name="tokenCacheProvider">The App token cache provider</param>
/// <param name="userTokenCacheProvider">The User token cache provider</param>
public TokenAcquisition(
IMsalAppTokenCacheProvider appTokenCacheProvider,
IMsalUserTokenCacheProvider userTokenCacheProvider,
IMsalTokenCacheProvider tokenCacheProvider,
IHttpContextAccessor httpContextAccessor,
IOptions<AzureADOptions> azureAdOptions,
IOptions<ConfidentialClientApplicationOptions> applicationOptions)
{
_httpContextAccessor = httpContextAccessor;
_azureAdOptions = azureAdOptions.Value;
_applicationOptions = applicationOptions.Value;
_appTokenCacheProvider = appTokenCacheProvider;
_userTokenCacheProvider = userTokenCacheProvider;
_tokenCacheProvider = tokenCacheProvider;
}

/// <summary>
Expand Down Expand Up @@ -283,7 +280,7 @@ public async Task RemoveAccountAsync(RedirectContext context)
if (account != null)
{
await app.RemoveAsync(account).ConfigureAwait(false);
_userTokenCacheProvider?.ClearAsync().ConfigureAwait(false);
_tokenCacheProvider?.ClearAsync().ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -326,8 +323,8 @@ private IConfidentialClientApplication BuildConfidentialClientApplication()
.Build();

// Initialize token cache providers
_appTokenCacheProvider?.InitializeAsync(app.AppTokenCache);
_userTokenCacheProvider?.InitializeAsync(app.UserTokenCache);
_tokenCacheProvider?.InitializeAsync(app.AppTokenCache);
_tokenCacheProvider?.InitializeAsync(app.UserTokenCache);

return app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IServiceCollection AddDistributedAppTokenCache(
this IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSingleton<IMsalAppTokenCacheProvider, MsalAppDistributedTokenCacheProvider>();
services.AddSingleton<IMsalTokenCacheProvider, MsalDistributedTokenCacheAdapter>();
return services;
}

Expand All @@ -42,7 +42,7 @@ public static IServiceCollection AddDistributedUserTokenCache(
{
services.AddDistributedMemoryCache();
services.AddHttpContextAccessor();
services.AddSingleton<IMsalUserTokenCacheProvider, MsalPerUserDistributedTokenCacheProvider>();
services.AddSingleton<IMsalTokenCacheProvider, MsalDistributedTokenCacheAdapter>();
return services;
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IMsalTokenCacheProvider
/// <param name="isAppTokenCache">Is the token cache an App token cache or
/// a user token cache</param>
/// <returns></returns>
Task InitializeAsync(ITokenCache tokenCache, bool isAppTokenCache);
Task InitializeAsync(ITokenCache tokenCache);

/// <summary>
/// Clear the cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,10 @@ public static class InMemoryTokenCacheProviderExtension
/// <returns></returns>
public static IServiceCollection AddInMemoryTokenCaches(
this IServiceCollection services)
{
AddInMemoryAppTokenCache(services);
AddInMemoryPerUserTokenCache(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 AddInMemoryAppTokenCache(
this IServiceCollection services)
{
services.AddMemoryCache();
services.AddSingleton<IMsalAppTokenCacheProvider, MsalAppMemoryTokenCacheProvider>();
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 AddInMemoryPerUserTokenCache(
this IServiceCollection services)
{
services.AddMemoryCache();
services.AddHttpContextAccessor();
services.AddSingleton<IMsalUserTokenCacheProvider, MsalPerUserMemoryTokenCacheProvider>();
services.AddSingleton<IMsalTokenCacheProvider, MsalMemoryTokenCacheProvider>();
return services;
}
}
Expand Down

This file was deleted.

This file was deleted.

Loading