Skip to content

Commit

Permalink
Need to pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Feb 1, 2024
1 parent e9ed863 commit 737388b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 32 deletions.
5 changes: 5 additions & 0 deletions AzureAd/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.exclude": {
"**/.vs": false
}
}
2 changes: 1 addition & 1 deletion AzureAd/AppType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum AppType

WebUiBased = Web | RazorPages | Mvc,

WebBased = Web | RazorPages | Mvc | WebApi | AzureFunction | AzureWebJob,
WebBased = ApiBased | WebUiBased,

All =
Web
Expand Down
5 changes: 3 additions & 2 deletions AzureAd/AzureAdApplicationBuilderIdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ public static IApplicationBuilder UseAzureAdB2CIdentity(this IApplicationBuilder
var mvcOptions = app.ApplicationServices
.GetService<IOptions<Dgmjr.AspNetCore.Mvc.MvcOptions>>()
?.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();
});
Expand Down
39 changes: 25 additions & 14 deletions AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -59,23 +60,33 @@ public static WebApplicationBuilder AddAzureAdB2CIdentity(this WebApplicationBui
);
}

var msGraphOptionsConfigSection = builder.Configuration.GetSection(DownstreamApis_MsGraphConfigurationKey);
var msGraphOptions = msGraphOptionsConfigSection.Get<AzureAdB2CGraphOptions>();

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()
)
{
Expand Down
1 change: 1 addition & 0 deletions AzureAd/Dgmjr.AzureAd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageReference Include="Dgmjr.Configuration.Extensions" />
<PackageReference Include="Dgmjr.Http.Headers" />
<PackageReference Include="Dgmjr.Mime" />
<PackageReference Include="Dgmjr.Graph" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Identity.Abstractions" />
Expand Down
26 changes: 13 additions & 13 deletions AzureAd/Dgmjr.AzureAd.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions AzureAd/DownstreamApiOptionsConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Dgmjr.AzureAd;
using System.Net.Http;
using Application = Dgmjr.Mime.Application;

public class DownstreamApiOptionsConfigurator(IOptionsMonitor<JsonOptions> jsonOptions) : IConfigureOptions<DownstreamApiOptions>
{
Expand Down
2 changes: 1 addition & 1 deletion AzureAd/MicrosoftIdentityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
public ICollection<string> InitialScopes => Scope;
}
2 changes: 1 addition & 1 deletion Logging/AutomaticLoggingConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AutomaticLoggingConfigurator
: IConfigureIHostApplicationBuilder,
IConfigureIApplicationBuilder
{
public ConfigurationOrder Order => ConfigurationOrder.VeryEarly;
public ConfigurationOrder Order => ConfigurationOrder.First;

public void Configure(WebApplicationBuilder builder)
{
Expand Down

0 comments on commit 737388b

Please sign in to comment.