From 5f191c2a10369326d8d4a9ba4a710fe73b189625 Mon Sep 17 00:00:00 2001 From: Jean-Marc Prieur Date: Tue, 3 Oct 2023 16:18:26 -0700 Subject: [PATCH] Fixes #2410, #2410 (#2499) * Fixes #2410 --- .../ServiceCollectionExtensions.cs | 2 +- .../TestTokenAcquisitionHost.cs | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs diff --git a/src/Microsoft.Identity.Web.TokenAcquisition/ServiceCollectionExtensions.cs b/src/Microsoft.Identity.Web.TokenAcquisition/ServiceCollectionExtensions.cs index c7f01245c..e04299551 100644 --- a/src/Microsoft.Identity.Web.TokenAcquisition/ServiceCollectionExtensions.cs +++ b/src/Microsoft.Identity.Web.TokenAcquisition/ServiceCollectionExtensions.cs @@ -42,7 +42,7 @@ public static IServiceCollection AddTokenAcquisition( _ = Throws.IfNull(services); #if !NETSTANDARD2_0 && !NET462 && !NET472 - bool forceSdk = !services.Any(s => s.ServiceType == typeof(AspNetCore.Hosting.IWebHostEnvironment)); + bool forceSdk = !services.Any(s => s.ServiceType.FullName == "Microsoft.AspNetCore.Authentication.IAuthenticationService"); #endif if (services.FirstOrDefault(s => s.ImplementationType == typeof(ICredentialsLoader)) == null) diff --git a/tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs b/tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs new file mode 100644 index 000000000..6f0f07eb1 --- /dev/null +++ b/tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Linq; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.Identity.Web.Test +{ + public class TestTokenAcquisitionHost + { + [Fact] + public void TestTokenAcquisitionHostNotAspNetCore() + { + // When not adding Services.AddAuthentication, the host should be + TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance(); + var host = tokenAcquirerFactory.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost"); + Assert.True(host.ImplementationType!.FullName == "Microsoft.Identity.Web.Hosts.DefaultTokenAcquisitionHost"); + } + + [Fact] + public void TestTokenAcquisitionAspNetCoreBuilderNoAuth() + { + // When not adding Services.AddAuthentication, the host should be + var builder = WebApplication.CreateBuilder(); + builder.Services.AddTokenAcquisition(); + var host = builder.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost"); + Assert.True(host.ImplementationType!.FullName == "Microsoft.Identity.Web.Hosts.DefaultTokenAcquisitionHost"); + } + + + [Fact] + public void TestTokenAcquisitionAspNetCoreBuilderAuth() + { + // When not adding Services.AddAuthentication, the host should be + var builder = WebApplication.CreateBuilder(); + builder.Services.AddAuthentication() + .AddMicrosoftIdentityWebApi(builder.Configuration) + .EnableTokenAcquisitionToCallDownstreamApi(); + + var host = builder.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost"); + Assert.True(host.ImplementationType!.FullName == "Microsoft.Identity.Web.TokenAcquisitionAspnetCoreHost"); + } + } +}