From 51af003c90604e165e0a9a1cea806e43398dbd3e Mon Sep 17 00:00:00 2001 From: jennyf19 Date: Thu, 2 Sep 2021 19:08:09 -0700 Subject: [PATCH] fix for anonymous controller (#1425) --- .../TokenAcquisition.cs | 7 +++++ .../AcquireTokenForAppIntegrationTests.cs | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/Microsoft.Identity.Web/TokenAcquisition.cs b/src/Microsoft.Identity.Web/TokenAcquisition.cs index 35059b1e3..448799e7c 100644 --- a/src/Microsoft.Identity.Web/TokenAcquisition.cs +++ b/src/Microsoft.Identity.Web/TokenAcquisition.cs @@ -358,6 +358,13 @@ public Task GetAuthenticationResultForAppAsync( authenticationScheme = GetEffectiveAuthenticationScheme(authenticationScheme); MergedOptions mergedOptions = GetOptions(authenticationScheme); + // Case of an anonymous controller, no [Authorize] attribute will trigger the merge options + if (string.IsNullOrEmpty(mergedOptions.Instance)) + { + var mergedOptionsMonitor = _serviceProvider.GetRequiredService>(); + mergedOptionsMonitor.Get(JwtBearerDefaults.AuthenticationScheme); + } + if (string.IsNullOrEmpty(tenant)) { tenant = mergedOptions.TenantId; diff --git a/tests/Microsoft.Identity.Web.Test.Integration/AcquireTokenForAppIntegrationTests.cs b/tests/Microsoft.Identity.Web.Test.Integration/AcquireTokenForAppIntegrationTests.cs index 4ae17fdb0..3f108b7a0 100644 --- a/tests/Microsoft.Identity.Web.Test.Integration/AcquireTokenForAppIntegrationTests.cs +++ b/tests/Microsoft.Identity.Web.Test.Integration/AcquireTokenForAppIntegrationTests.cs @@ -2,11 +2,14 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -214,6 +217,33 @@ async Task authResult() => Assert.Equal(0, _msalTestTokenCacheProvider.Count); } + [Fact] + public async Task GetAccessTokenForApp_WithAnonymousController_Async() + { + var serviceCollection = new ServiceCollection(); + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + { "AzureAd:Instance", "https://login.microsoftonline.com/" }, + { "AzureAd:TenantId", TestConstants.ConfidentialClientLabTenant }, + { "AzureAd:ClientId", TestConstants.ConfidentialClientId }, + { "AzureAd:ClientSecret", _ccaSecret }, + }) + .Build(); + serviceCollection.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddMicrosoftIdentityWebApi(configuration) + .EnableTokenAcquisitionToCallDownstreamApi() + .AddInMemoryTokenCaches(); + + var services = serviceCollection.BuildServiceProvider(); + + var tokenAcquisition = services.GetRequiredService(); + + var token = await tokenAcquisition.GetAccessTokenForAppAsync("https://graph.microsoft.com/.default").ConfigureAwait(false); + + Assert.NotNull(token); + } + private void InitializeTokenAcquisitionObjects() { MergedOptions mergedOptions = _provider.GetRequiredService>().Get(OpenIdConnectDefaults.AuthenticationScheme);