Skip to content

Commit

Permalink
Fixes #1390
Browse files Browse the repository at this point in the history
  • Loading branch information
jmprieur committed Aug 17, 2021
1 parent 1f46bd9 commit 1d70819
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Client;
Expand All @@ -18,6 +20,9 @@ namespace Microsoft.Identity.Web
/// </summary>
public static class TokenCacheExtensions
{
private static readonly Dictionary<MethodInfo, IServiceProvider> s_serviceProviderFromAction
= new Dictionary<MethodInfo, IServiceProvider>();

/// <summary>
/// Use a token cache and choose the serialization part by adding it to
/// the services collection and configuring its options.
Expand Down Expand Up @@ -75,15 +80,21 @@ internal static IConfidentialClientApplication AddTokenCaches(
throw new ArgumentNullException(nameof(initializeCaches));
}

IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
.ConfigureLogging(logger => { })
.ConfigureServices(services =>
{
initializeCaches(services);
services.AddDataProtection();
});
// Maintain a dictionary of service providers per `initializeCaches` delegate.
if (!s_serviceProviderFromAction.TryGetValue(initializeCaches.Method, out IServiceProvider? serviceProvider))
{
IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
.ConfigureLogging(logger => { })
.ConfigureServices(services =>
{
initializeCaches(services);
services.AddDataProtection();
});

serviceProvider = hostBuilder.Build().Services;
s_serviceProviderFromAction.Add(initializeCaches.Method, serviceProvider);
}

IServiceProvider serviceProvider = hostBuilder.Build().Services;
IMsalTokenCacheProvider msalTokenCacheProvider = serviceProvider.GetRequiredService<IMsalTokenCacheProvider>();
msalTokenCacheProvider.Initialize(confidentialClientApp.UserTokenCache);
msalTokenCacheProvider.Initialize(confidentialClientApp.AppTokenCache);
Expand Down

0 comments on commit 1d70819

Please sign in to comment.