Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -44,20 +45,20 @@ public static IServiceCollection AddTokenAcquisition(
bool forceSdk = !services.Any(s => s.ServiceType.FullName == "Microsoft.AspNetCore.Authentication.IAuthenticationService");
#endif

if (services.FirstOrDefault(s => s.ImplementationType == typeof(DefaultCertificateLoader)) == null)
if (!HasImplementationType(services, typeof(DefaultCertificateLoader)))
{
services.TryAddSingleton<ICredentialsLoader, DefaultCertificateLoader>();
}

if (services.FirstOrDefault(s => s.ImplementationType == typeof(MicrosoftIdentityOptionsMerger)) == null)
if (!HasImplementationType(services, typeof(MicrosoftIdentityOptionsMerger)))
{
services.TryAddSingleton<IPostConfigureOptions<MicrosoftIdentityOptions>, MicrosoftIdentityOptionsMerger>();
}
if (services.FirstOrDefault(s => s.ImplementationType == typeof(MicrosoftIdentityApplicationOptionsMerger)) == null)
if (!HasImplementationType(services, typeof(MicrosoftIdentityApplicationOptionsMerger)))
{
services.TryAddSingleton<IPostConfigureOptions<MicrosoftIdentityApplicationOptions>, MicrosoftIdentityApplicationOptionsMerger>();
}
if (services.FirstOrDefault(s => s.ImplementationType == typeof(ConfidentialClientApplicationOptionsMerger)) == null)
if (!HasImplementationType(services, typeof(ConfidentialClientApplicationOptionsMerger)))
{
services.TryAddSingleton<IPostConfigureOptions<ConfidentialClientApplicationOptions>, ConfidentialClientApplicationOptionsMerger>();
}
Expand Down Expand Up @@ -139,5 +140,14 @@ public static IServiceCollection AddTokenAcquisition(
services.AddSingleton<IMergedOptionsStore, MergedOptionsStore>();
return services;
}

private static bool HasImplementationType(IServiceCollection services, Type implementationType)
{
return services.Any(s =>
#if NET8_0_OR_GREATER
s.ServiceKey is null &&
#endif
s.ImplementationType == implementationType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ private static void AddMicrosoftIdentityWebApiImplementation(
builder.Services.AddRequiredScopeAuthorization();
builder.Services.AddRequiredScopeOrAppPermissionAuthorization();
builder.Services.AddOptions<AadIssuerValidatorOptions>();
if (builder.Services.FirstOrDefault(s => s.ImplementationType == typeof(MicrosoftIdentityOptionsMerger)) == null)
if (!HasImplementationType(builder.Services, typeof(MicrosoftIdentityOptionsMerger)))
{
builder.Services.TryAddSingleton<IPostConfigureOptions<MicrosoftIdentityOptions>, MicrosoftIdentityOptionsMerger>();
}
if (builder.Services.FirstOrDefault(s => s.ImplementationType == typeof(JwtBearerOptionsMerger)) == null)
if (!HasImplementationType(builder.Services, typeof(JwtBearerOptionsMerger)))
{
builder.Services.TryAddSingleton<IPostConfigureOptions<JwtBearerOptions>, JwtBearerOptionsMerger>();
}
Expand Down Expand Up @@ -308,5 +308,14 @@ internal static void ChainOnTokenValidatedEventForClaimsValidation(JwtBearerEven
await tokenValidatedHandler(context).ConfigureAwait(false);
};
}

private static bool HasImplementationType(IServiceCollection services, Type implementationType)
{
return services.Any(s =>
#if NET8_0_OR_GREATER
s.ServiceKey is null &&
#endif
s.ImplementationType == implementationType);
}
}
}
Loading