Skip to content

Commit 086bf8d

Browse files
authored
Fix #2456 (#2489)
* Fix build on .NET FW after #2480 * Fix the "The referenced project is targeted to a different framework family" warnings by changing the order of the frameworks (older to newer) as advised in https://www.primordialcode.com/blog/post/referenced-project-targeted-different-framework-family * Fixes #2456 * Addressing comments
1 parent 768d105 commit 086bf8d

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

Directory.Build.props

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
</PropertyGroup>
2121

2222
<PropertyGroup>
23-
<TargetFrameworks Condition="'$(TargetNet8)' == 'True'">net462; net472; netstandard2.0; netcoreapp3.1; net6.0; net7.0; net8.0;</TargetFrameworks>
24-
<TargetFrameworks Condition="'$(TargetNet8)' != 'True'">net462; net472; netstandard2.0; netcoreapp3.1; net6.0; net7.0;</TargetFrameworks>
23+
<!-- For files to appear in the Visual Studio Solution explorer given we have conditional inclusion in some projects (IdWeb for instance)
24+
we need to have the higher framework, even if this produces a warning in the IDE -->
25+
<TargetFrameworks Condition="'$(TargetNet8)' == 'True'">net7.0; net462; net472; netstandard2.0; netcoreapp3.1; net6.0; net8.0;</TargetFrameworks>
26+
<TargetFrameworks Condition="'$(TargetNet8)' != 'True'">net7.0; net462; net472; netstandard2.0; netcoreapp3.1; net6.0;</TargetFrameworks>
2527
<SignAssembly>true</SignAssembly>
2628
<AssemblyOriginatorKeyFile>../../build/MSAL.snk</AssemblyOriginatorKeyFile>
2729
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityWebAppAuthenticationBuilder.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
88
using System.Security.Claims;
9+
using System.Threading.Tasks;
910
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
1011
using Microsoft.Extensions.Configuration;
1112
using Microsoft.Extensions.DependencyInjection;
@@ -79,7 +80,7 @@ public MicrosoftIdentityAppCallsWebApiAuthenticationBuilder EnableTokenAcquisiti
7980
WebAppCallsWebApiImplementation(
8081
Services,
8182
initialScopes,
82-
ConfigureMicrosoftIdentityOptions,
83+
null, /* to avoid calling the delegate twice */
8384
OpenIdConnectScheme,
8485
configureConfidentialClientApplicationOptions);
8586
return new MicrosoftIdentityAppCallsWebApiAuthenticationBuilder(
@@ -93,14 +94,21 @@ public MicrosoftIdentityAppCallsWebApiAuthenticationBuilder EnableTokenAcquisiti
9394
internal static void WebAppCallsWebApiImplementation(
9495
IServiceCollection services,
9596
IEnumerable<string>? initialScopes,
96-
Action<MicrosoftIdentityOptions> configureMicrosoftIdentityOptions,
97+
Action<MicrosoftIdentityOptions>? configureMicrosoftIdentityOptions,
9798
string openIdConnectScheme,
9899
Action<ConfidentialClientApplicationOptions>? configureConfidentialClientApplicationOptions)
99100
{
100-
// Ensure that configuration options for MSAL.NET, HttpContext accessor and the Token acquisition service
101-
// (encapsulating MSAL.NET) are available through dependency injection
102-
services.Configure(openIdConnectScheme, configureMicrosoftIdentityOptions);
103-
101+
// When called from MISE, ensure that configuration options for MSAL.NET, HttpContext accessor
102+
// and the Token acquisition service (encapsulating MSAL.NET) are available through dependency injection.
103+
// When called from AddMicrosoftIdentityWebApp(delegate), should not be re-configured otherwise
104+
// the delegate would be called twice.
105+
if (configureMicrosoftIdentityOptions != null)
106+
{
107+
// Won't be null in the case where the caller is MISE (to ensure that the configuration for MSAL.NET
108+
// is available through DI).
109+
// Will be null when called from AddMicrosoftIdentityWebApp(delegate) to avoid calling the delegate twice.
110+
services.Configure(openIdConnectScheme, configureMicrosoftIdentityOptions);
111+
}
104112
if (configureConfidentialClientApplicationOptions != null)
105113
{
106114
services.Configure(openIdConnectScheme, configureConfidentialClientApplicationOptions);
@@ -157,8 +165,7 @@ internal static void WebAppCallsWebApiImplementation(
157165
};
158166

159167
// Handling the token validated to get the client_info for cases where tenantId is not present (example: B2C)
160-
var onTokenValidatedHandler = options.Events.OnTokenValidated;
161-
options.Events.OnTokenValidated = async context =>
168+
options.Events.OnTokenValidated += async context =>
162169
{
163170
string? clientInfo = context!.ProtocolMessage?.GetParameter(ClaimConstants.ClientInfo);
164171

@@ -172,8 +179,7 @@ internal static void WebAppCallsWebApiImplementation(
172179
context!.Principal!.Identities.FirstOrDefault()?.AddClaim(new Claim(ClaimConstants.UniqueObjectIdentifier, clientInfoFromServer.UniqueObjectIdentifier));
173180
}
174181
}
175-
176-
await onTokenValidatedHandler(context).ConfigureAwait(false);
182+
await Task.CompletedTask;
177183
};
178184

179185
// Handling the sign-out: removing the account from MSAL.NET cache

tests/DevApps/WebAppCallsMicrosoftGraph/Startup.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// Licensed under the MIT License.
33

44
// #define USE_SIGNED_ASSERTION
5+
using System;
56
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
67
using Microsoft.AspNetCore.Authorization;
78
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Connections;
810
using Microsoft.AspNetCore.Hosting;
911
using Microsoft.AspNetCore.Mvc.Authorization;
1012
using Microsoft.Extensions.Configuration;
@@ -34,9 +36,24 @@ public void ConfigureServices(IServiceCollection services)
3436
string configSection = "AzureAd";
3537
#endif
3638

37-
39+
int count = 0;
3840
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
39-
.AddMicrosoftIdentityWebApp(Configuration.GetSection(configSection))
41+
.AddMicrosoftIdentityWebApp(options =>
42+
{
43+
// Verification of the fix for #2456
44+
if (count>0)
45+
{
46+
throw new ArgumentException("AddMicrosoftIdentityWebApp(delegate). the delegate" +
47+
"is called more than once");
48+
}
49+
else
50+
{
51+
count++;
52+
}
53+
54+
Configuration.Bind(configSection, options);
55+
}
56+
)
4057
.EnableTokenAcquisitionToCallDownstreamApi()
4158
.AddMicrosoftGraph(Configuration.GetSection("GraphBeta"))
4259
.AddDownstreamApi("GraphBeta", Configuration.GetSection("GraphBeta"))

0 commit comments

Comments
 (0)