Skip to content

Commit

Permalink
Fixes #1390 (#1391)
Browse files Browse the repository at this point in the history
* Fixes #1390

* Addressing Bogdan's PR feedback

* address PR comment (#1393)

Co-authored-by: jennyf19 <jeferrie@microsoft.com>
  • Loading branch information
jmprieur and jennyf19 authored Aug 18, 2021
1 parent f2f6c9d commit bd087e1
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Client;
Expand All @@ -18,6 +21,9 @@ namespace Microsoft.Identity.Web
/// </summary>
public static class TokenCacheExtensions
{
private static readonly IDictionary<MethodInfo, IServiceProvider> s_serviceProviderFromAction
= new ConcurrentDictionary<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 +81,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[initializeCaches.Method] = serviceProvider;
}

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

1 comment on commit bd087e1

@bgavrilMS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is preventing you from adding unit tests to this kind of functionality?

Please sign in to comment.