|
6 | 6 | using System.Net.Http; |
7 | 7 | using System.Threading; |
8 | 8 | using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| 10 | +using Microsoft.Extensions.Caching.Memory; |
9 | 11 | using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +using Microsoft.Extensions.Options; |
10 | 14 | using Microsoft.Identity.Abstractions; |
11 | 15 | using Microsoft.Identity.Client; |
12 | 16 | using Microsoft.Identity.Web.Test.Common; |
13 | 17 | using Microsoft.Identity.Web.Test.Common.Mocks; |
| 18 | +using Microsoft.Identity.Web.Test.Common.TestHelpers; |
14 | 19 | using Microsoft.Identity.Web.TestOnly; |
| 20 | +using Microsoft.Identity.Web.TokenCacheProviders.InMemory; |
15 | 21 | using Xunit; |
16 | | - |
| 22 | +using TC = Microsoft.Identity.Web.Test.Common.TestConstants; |
17 | 23 |
|
18 | 24 | namespace Microsoft.Identity.Web.Test |
19 | 25 | { |
@@ -126,6 +132,71 @@ public async Task ExtraBodyParametersAreSentToEndpointTest() |
126 | 132 | Assert.Equal("Bearer header.payload.signature", result); |
127 | 133 | } |
128 | 134 |
|
| 135 | + // https://github.com/AzureAD/microsoft-identity-web/issues/3237 |
| 136 | + [Fact] |
| 137 | + public async Task TokenAcquisitionUsesMemoryCacheWhenConfigured() |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + var microsoftIdentityOptionsMonitor = new TestOptionsMonitor<MicrosoftIdentityOptions>(new MicrosoftIdentityOptions |
| 141 | + { |
| 142 | + Authority = TC.AuthorityCommonTenant, |
| 143 | + ClientId = TC.ConfidentialClientId, |
| 144 | + CallbackPath = string.Empty, |
| 145 | + }); |
| 146 | + |
| 147 | + var applicationOptionsMonitor = new TestOptionsMonitor<ConfidentialClientApplicationOptions>(new ConfidentialClientApplicationOptions |
| 148 | + { |
| 149 | + Instance = TC.AadInstance, |
| 150 | + RedirectUri = "https://localhost:1234", |
| 151 | + ClientSecret = TC.ClientSecret, |
| 152 | + }); |
| 153 | + |
| 154 | + var services = new ServiceCollection(); |
| 155 | + services.AddTransient( |
| 156 | + provider => microsoftIdentityOptionsMonitor); |
| 157 | + services.AddTransient( |
| 158 | + provider => applicationOptionsMonitor); |
| 159 | + services.Configure<MergedOptions>(options => { }); |
| 160 | + services.AddTokenAcquisition(); |
| 161 | + services.AddLogging(); |
| 162 | + services.AddAuthentication(); |
| 163 | + services.AddSingleton<IMsalHttpClientFactory, MockHttpClientFactory>(); |
| 164 | + services.AddMemoryCache(); |
| 165 | + var provider = services.BuildServiceProvider(); |
| 166 | + |
| 167 | + |
| 168 | + MergedOptions mergedOptions = provider.GetRequiredService<IMergedOptionsStore>().Get(OpenIdConnectDefaults.AuthenticationScheme); |
| 169 | + MergedOptions.UpdateMergedOptionsFromMicrosoftIdentityOptions(microsoftIdentityOptionsMonitor.Get(OpenIdConnectDefaults.AuthenticationScheme), mergedOptions); |
| 170 | + MergedOptions.UpdateMergedOptionsFromConfidentialClientApplicationOptions(applicationOptionsMonitor.Get(OpenIdConnectDefaults.AuthenticationScheme), mergedOptions); |
| 171 | + |
| 172 | + var credentialsLoader = new DefaultCredentialsLoader(); |
| 173 | + var tokenAcquisitionAspnetCoreHost = new TokenAcquisitionAspnetCoreHost( |
| 174 | + MockHttpContextAccessor.CreateMockHttpContextAccessor(), |
| 175 | + provider.GetService<IMergedOptionsStore>()!, |
| 176 | + provider); |
| 177 | + var tokenAcquisition = new TokenAcquisitionAspNetCore( |
| 178 | + new MsalTestTokenCacheProvider( |
| 179 | + provider.GetService<IMemoryCache>()!, |
| 180 | + provider.GetService<IOptions<MsalMemoryTokenCacheOptions>>()!), |
| 181 | + provider.GetService<IHttpClientFactory>()!, |
| 182 | + provider.GetService<ILogger<TokenAcquisition>>()!, |
| 183 | + tokenAcquisitionAspnetCoreHost, |
| 184 | + provider, |
| 185 | + credentialsLoader); |
| 186 | + var mockHttpClient = provider.GetRequiredService<IMsalHttpClientFactory>() as MockHttpClientFactory; |
| 187 | + |
| 188 | + IConfidentialClientApplication app = await tokenAcquisition.GetOrBuildConfidentialClientApplicationAsync(mergedOptions); |
| 189 | + mockHttpClient!.AddMockHandler(MockHttpCreator.CreateClientCredentialTokenHandler()); |
| 190 | + |
| 191 | + // Act |
| 192 | + var result = await app.AcquireTokenForClient(new[] { "r" }).ExecuteAsync(); |
| 193 | + |
| 194 | + // Assert |
| 195 | + Assert.Equal(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource); |
| 196 | + IMemoryCache memoryCache = provider.GetService<IMemoryCache>()!; |
| 197 | + Assert.Equal(1, (memoryCache as MemoryCache)!.Count); // this proves that MemoryCache is used |
| 198 | + } |
| 199 | + |
129 | 200 | private TokenAcquirerFactory InitTokenAcquirerFactory() |
130 | 201 | { |
131 | 202 | TokenAcquirerFactoryTesting.ResetTokenAcquirerFactoryInTest(); |
|
0 commit comments