Skip to content

Commit 5f191c2

Browse files
authored
Fixes #2410, #2410 (#2499)
* Fixes #2410
1 parent 826ff82 commit 5f191c2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Microsoft.Identity.Web.TokenAcquisition/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static IServiceCollection AddTokenAcquisition(
4242
_ = Throws.IfNull(services);
4343

4444
#if !NETSTANDARD2_0 && !NET462 && !NET472
45-
bool forceSdk = !services.Any(s => s.ServiceType == typeof(AspNetCore.Hosting.IWebHostEnvironment));
45+
bool forceSdk = !services.Any(s => s.ServiceType.FullName == "Microsoft.AspNetCore.Authentication.IAuthenticationService");
4646
#endif
4747

4848
if (services.FirstOrDefault(s => s.ImplementationType == typeof(ICredentialsLoader)) == null)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Linq;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Xunit;
8+
9+
namespace Microsoft.Identity.Web.Test
10+
{
11+
public class TestTokenAcquisitionHost
12+
{
13+
[Fact]
14+
public void TestTokenAcquisitionHostNotAspNetCore()
15+
{
16+
// When not adding Services.AddAuthentication, the host should be
17+
TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
18+
var host = tokenAcquirerFactory.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost");
19+
Assert.True(host.ImplementationType!.FullName == "Microsoft.Identity.Web.Hosts.DefaultTokenAcquisitionHost");
20+
}
21+
22+
[Fact]
23+
public void TestTokenAcquisitionAspNetCoreBuilderNoAuth()
24+
{
25+
// When not adding Services.AddAuthentication, the host should be
26+
var builder = WebApplication.CreateBuilder();
27+
builder.Services.AddTokenAcquisition();
28+
var host = builder.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost");
29+
Assert.True(host.ImplementationType!.FullName == "Microsoft.Identity.Web.Hosts.DefaultTokenAcquisitionHost");
30+
}
31+
32+
33+
[Fact]
34+
public void TestTokenAcquisitionAspNetCoreBuilderAuth()
35+
{
36+
// When not adding Services.AddAuthentication, the host should be
37+
var builder = WebApplication.CreateBuilder();
38+
builder.Services.AddAuthentication()
39+
.AddMicrosoftIdentityWebApi(builder.Configuration)
40+
.EnableTokenAcquisitionToCallDownstreamApi();
41+
42+
var host = builder.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost");
43+
Assert.True(host.ImplementationType!.FullName == "Microsoft.Identity.Web.TokenAcquisitionAspnetCoreHost");
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)