Skip to content

Commit

Permalink
Fixes #2410, #2410 (#2499)
Browse files Browse the repository at this point in the history
* Fixes #2410
  • Loading branch information
jmprieur authored Oct 3, 2023
1 parent 826ff82 commit 5f191c2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit 5f191c2

Please sign in to comment.