From 737388ba2fe5b2f265c23993a4603d4a561822cf Mon Sep 17 00:00:00 2001 From: "David G. Moore, Jr." Date: Thu, 1 Feb 2024 17:53:29 -0500 Subject: [PATCH] Need to pull request --- AzureAd/.vscode/settings.json | 5 +++ AzureAd/AppType.cs | 2 +- ...eAdApplicationBuilderIdentityExtensions.cs | 5 ++- ...ostApplicationBuilderIdentityExtensions.cs | 39 ++++++++++++------- AzureAd/Dgmjr.AzureAd.csproj | 1 + AzureAd/Dgmjr.AzureAd.sln | 26 ++++++------- AzureAd/DownstreamApiOptionsConfigurator.cs | 1 + AzureAd/MicrosoftIdentityOptions.cs | 2 +- Logging/AutomaticLoggingConfigurator.cs | 2 +- 9 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 AzureAd/.vscode/settings.json diff --git a/AzureAd/.vscode/settings.json b/AzureAd/.vscode/settings.json new file mode 100644 index 0000000..40ac7cb --- /dev/null +++ b/AzureAd/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.exclude": { + "**/.vs": false + } +} diff --git a/AzureAd/AppType.cs b/AzureAd/AppType.cs index 8a280ce..79187c8 100644 --- a/AzureAd/AppType.cs +++ b/AzureAd/AppType.cs @@ -20,7 +20,7 @@ public enum AppType WebUiBased = Web | RazorPages | Mvc, - WebBased = Web | RazorPages | Mvc | WebApi | AzureFunction | AzureWebJob, + WebBased = ApiBased | WebUiBased, All = Web diff --git a/AzureAd/AzureAdApplicationBuilderIdentityExtensions.cs b/AzureAd/AzureAdApplicationBuilderIdentityExtensions.cs index 32b8773..0352ed8 100644 --- a/AzureAd/AzureAdApplicationBuilderIdentityExtensions.cs +++ b/AzureAd/AzureAdApplicationBuilderIdentityExtensions.cs @@ -9,13 +9,14 @@ public static IApplicationBuilder UseAzureAdB2CIdentity(this IApplicationBuilder var mvcOptions = app.ApplicationServices .GetService>() ?.Value; + app.UseSession(); app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { - // if (mvcOptions?.AddControllers == true) - // endpoints.MapControllers(); + if (mvcOptions?.AddControllers == true) + endpoints.MapControllers(); if (mvcOptions?.AddRazorPages == true) endpoints.MapRazorPages(); }); diff --git a/AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs b/AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs index 2cfd1bd..4159c12 100644 --- a/AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs +++ b/AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs @@ -10,6 +10,7 @@ namespace Microsoft.Extensions.DependencyInjection; using Microsoft.Identity.Web.Resource; using Microsoft.Identity.Web.UI; using MicrosoftIdentityOptions = Dgmjr.AzureAd.Web.MicrosoftIdentityOptions; +using MsidCallsWebApiAuthBuilder = MicrosoftIdentityAppCallsWebApiAuthenticationBuilder; public static class AzureAdHostApplicationBuilderIdentityExtensions { @@ -36,21 +37,21 @@ public static WebApplicationBuilder AddAzureAdB2CIdentity(this WebApplicationBui var authenticationBuilder = builder.Services.AddAuthentication(OpenIdConnect); - MicrosoftIdentityAppCallsWebApiAuthenticationBuilder callsWebApiAuthenticationBuilder; - if ((options.AppType & AppType.WebBased) == options.AppType) + MsidCallsWebApiAuthBuilder callsWebApiAuthenticationBuilder; + if (AppType.WebUiBased.HasFlag(options.AppType)) { Console.WriteLine("Registering Microsoft Identity Web UI."); callsWebApiAuthenticationBuilder = authenticationBuilder - .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection(AzureAdB2C)) - .EnableTokenAcquisitionToCallDownstreamApi(options.Scope); + .AddMicrosoftIdentityWebApp(configurationSection) + .EnableTokenAcquisitionToCallDownstreamApi(opts => configurationSection.Bind(opts), options.Scope); builder.Services.AddMvc().AddMicrosoftIdentityUI(); } - else if ((options.AppType & AppType.ApiBased) == options.AppType) + else if (AppType.ApiBased.HasFlag(options.AppType)) { Console.WriteLine("Registering app with type {0}", options.AppType); callsWebApiAuthenticationBuilder = authenticationBuilder - .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection(AzureAdB2C)) - .EnableTokenAcquisitionToCallDownstreamApi(); + .AddMicrosoftIdentityWebApi(configurationSection) + .EnableTokenAcquisitionToCallDownstreamApi(opts => configurationSection.Bind(opts)); } else { @@ -59,23 +60,33 @@ public static WebApplicationBuilder AddAzureAdB2CIdentity(this WebApplicationBui ); } + var msGraphOptionsConfigSection = builder.Configuration.GetSection(DownstreamApis_MsGraphConfigurationKey); + var msGraphOptions = msGraphOptionsConfigSection.Get(); + authenticationBuilder.AddJwtBearer( JwtBearerSchemeName, JwtBearerSchemeDisplayName, options => configurationSection.Bind(options) ); - callsWebApiAuthenticationBuilder - .AddMicrosoftGraph( - builder.Configuration.GetSection(DownstreamApis_MsGraphConfigurationKey) - ) - .AddDistributedTokenCaches(); + if(msGraphOptions.AppOnly) + { + callsWebApiAuthenticationBuilder + .AddMicrosoftGraphAppOnly(authProvider => new GraphServiceClient(authProvider)) + .AddDistributedTokenCaches(); + } + else + { + callsWebApiAuthenticationBuilder + .AddMicrosoftGraph(msGraphOptionsConfigSection) + .AddDistributedTokenCaches(); + } - callsWebApiAuthenticationBuilder.AddSessionTokenCaches(); + // callsWebApiAuthenticationBuilder.AddSessionTokenCaches(); foreach ( var downstreamApiConfig in builder.Configuration - .GetSection(DownstreamApis) + .GetSection(Dgmjr.AzureAd.Constants.DownstreamApis) .GetChildren() ) { diff --git a/AzureAd/Dgmjr.AzureAd.csproj b/AzureAd/Dgmjr.AzureAd.csproj index 3a20818..3aecf08 100644 --- a/AzureAd/Dgmjr.AzureAd.csproj +++ b/AzureAd/Dgmjr.AzureAd.csproj @@ -8,6 +8,7 @@ + diff --git a/AzureAd/Dgmjr.AzureAd.sln b/AzureAd/Dgmjr.AzureAd.sln index 7b4bae4..35acfa9 100644 --- a/AzureAd/Dgmjr.AzureAd.sln +++ b/AzureAd/Dgmjr.AzureAd.sln @@ -8,7 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution ..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\Packages\Versions.Local.props EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.AzureAd", "Dgmjr.AzureAd.csproj", "{C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.AzureAd", "Dgmjr.AzureAd.csproj", "{6C87F893-EEB5-409F-A3D9-6AB329FABAE0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -20,18 +20,18 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Local|Any CPU.ActiveCfg = Local|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Local|Any CPU.Build.0 = Local|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Testing|Any CPU.ActiveCfg = Testing|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Testing|Any CPU.Build.0 = Testing|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Staging|Any CPU.ActiveCfg = Staging|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Staging|Any CPU.Build.0 = Staging|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Production|Any CPU.ActiveCfg = Local|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Production|Any CPU.Build.0 = Local|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C963A627-CDCB-4EFA-AFDB-B8ABFB7AD1DB}.Release|Any CPU.Build.0 = Release|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Local|Any CPU.ActiveCfg = Local|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Local|Any CPU.Build.0 = Local|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Testing|Any CPU.ActiveCfg = Testing|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Testing|Any CPU.Build.0 = Testing|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Staging|Any CPU.ActiveCfg = Staging|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Staging|Any CPU.Build.0 = Staging|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Production|Any CPU.ActiveCfg = Local|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Production|Any CPU.Build.0 = Local|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C87F893-EEB5-409F-A3D9-6AB329FABAE0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AzureAd/DownstreamApiOptionsConfigurator.cs b/AzureAd/DownstreamApiOptionsConfigurator.cs index 90aa315..e0e73dc 100644 --- a/AzureAd/DownstreamApiOptionsConfigurator.cs +++ b/AzureAd/DownstreamApiOptionsConfigurator.cs @@ -1,5 +1,6 @@ namespace Dgmjr.AzureAd; using System.Net.Http; +using Application = Dgmjr.Mime.Application; public class DownstreamApiOptionsConfigurator(IOptionsMonitor jsonOptions) : IConfigureOptions { diff --git a/AzureAd/MicrosoftIdentityOptions.cs b/AzureAd/MicrosoftIdentityOptions.cs index c1ffb10..082aaca 100644 --- a/AzureAd/MicrosoftIdentityOptions.cs +++ b/AzureAd/MicrosoftIdentityOptions.cs @@ -4,5 +4,5 @@ public class MicrosoftIdentityOptions : Microsoft.Identity.Web.MicrosoftIdentity { public AppType AppType { get; set; } = AppType.WebApi; public string DefaultFallbackRoute { get; set; } = "/index"; - public string[] InitialScopes { get; set; } = Empty(); + public ICollection InitialScopes => Scope; } diff --git a/Logging/AutomaticLoggingConfigurator.cs b/Logging/AutomaticLoggingConfigurator.cs index 79c8e9a..6dfe379 100644 --- a/Logging/AutomaticLoggingConfigurator.cs +++ b/Logging/AutomaticLoggingConfigurator.cs @@ -11,7 +11,7 @@ public class AutomaticLoggingConfigurator : IConfigureIHostApplicationBuilder, IConfigureIApplicationBuilder { - public ConfigurationOrder Order => ConfigurationOrder.VeryEarly; + public ConfigurationOrder Order => ConfigurationOrder.First; public void Configure(WebApplicationBuilder builder) {