From d1aa9d1ab5349a89cd405e032e36c55bc4127821 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:18:51 +0100 Subject: [PATCH 01/16] .NET 9 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 09680f3..520ee3e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ ## History +- 2025-01-02 .NET 9 - 2024-04-11 Updated packages - 2023-12-01 Updated .NET 8 - 2023-08-14 Updated downstream APIs solution From aa13e6e87fc59262f4c7e28ac7c707bb78e7b5e1 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:19:45 +0100 Subject: [PATCH 02/16] Update build .NET 9 --- .github/workflows/dotnet.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index fa39c8f..c9de30b 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -12,11 +12,11 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v4 with: - dotnet-version: '8.0' + dotnet-version: '9.0' include-prerelease: True - name: Build AngularMicrosoftEntraIDMultipleApis run: dotnet build ./AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis.sln From 7facb5341eafd2c487397cde7c8339a064d86802 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:20:21 +0100 Subject: [PATCH 03/16] .NET 9 projects --- .../ApiWithMutlipleApis/ApiWithMutlipleApis.csproj | 2 +- .../ServiceApi/ServiceApi.csproj | 2 +- AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj index 2c7c869..334399a 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj @@ -1,7 +1,7 @@  - net8.0 + net9.0 3e9ac996-8e75-4dd9-9a5b-27a6e01a3f3d enable enable diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj index 365b465..971c524 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj @@ -1,7 +1,7 @@  - net8.0 + net9.0 196b270c-b0c0-4b90-8f04-d3108e701d51 enable diff --git a/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj b/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj index 82a411a..fb73a78 100644 --- a/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj @@ -1,7 +1,7 @@  - net8.0 + net9.0 aspnet-UserApiOne-2ba0bbf9-49f5-452e-8333-1e33a467b74e enable enable From 20ea5961e36403fa037b867e9f9b0cbc147549fb Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:21:15 +0100 Subject: [PATCH 04/16] Updated packages --- .../ApiWithMutlipleApis.csproj | 30 +++++++++---------- .../ServiceApi/ServiceApi.csproj | 28 ++++++++--------- .../UserApi/UserApi.csproj | 4 +-- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj index 334399a..1ffb387 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj @@ -8,25 +8,25 @@ - - - + + + - - - + + + - - - - - - - - + + + + + + + + - + diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj index 971c524..a25c47f 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj @@ -7,24 +7,24 @@ - + - - - - + + + + - - - - - - - - + + + + + + + + - + diff --git a/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj b/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj index fb73a78..4665d40 100644 --- a/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj @@ -8,8 +8,8 @@ - - + + From 772497703e9ca2593b380a5daa199ba41673c79c Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:25:50 +0100 Subject: [PATCH 05/16] code clean up --- .../Controllers/GraphApiCallsController.cs | 8 ++++---- .../ApiWithMutlipleApis/Program.cs | 16 ++++++++-------- .../Services/ServiceApiClientService.cs | 2 +- .../Services/UserApiClientService.cs | 10 +++------- ...HostingExtensions.cs => StartupExtensions.cs} | 4 ++-- .../Controllers/ApiForServiceDataController.cs | 4 ++-- .../ServiceApi/HostingExtensions.cs | 2 +- .../Policies/HasServiceApiRoleHandler.cs | 2 +- .../ServiceApi/Program.cs | 4 ++-- .../Controllers/ApiForUserDataController.cs | 2 +- .../UserApi/Program.cs | 1 - 11 files changed, 25 insertions(+), 30 deletions(-) rename AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/{HostingExtensions.cs => StartupExtensions.cs} (98%) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/GraphApiCallsController.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/GraphApiCallsController.cs index 4778259..782b6d0 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/GraphApiCallsController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/GraphApiCallsController.cs @@ -7,12 +7,12 @@ namespace ApiWithMutlipleApis.Controllers; [Authorize(Policy = "ValidateAccessTokenPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -[AuthorizeForScopes(Scopes = new string[] { "User.ReadBasic.All", "user.read" })] +[AuthorizeForScopes(Scopes = ["User.ReadBasic.All", "user.read"])] [ApiController] [Route("[controller]")] public class GraphApiCallsController : ControllerBase { - private GraphApiClientService _graphApiClientService; + private readonly GraphApiClientService _graphApiClientService; public GraphApiCallsController(GraphApiClientService graphApiClientService) { @@ -24,7 +24,7 @@ public async Task> Get() { var userData = await _graphApiClientService.GetGraphApiUser(); - return new List { $"DisplayName: {userData.DisplayName}", - $"GivenName: {userData.GivenName}", $"AboutMe: {userData.AboutMe}" }; + return [ $"DisplayName: {userData!.DisplayName}", + $"GivenName: {userData!.GivenName}", $"AboutMe: {userData!.AboutMe}" ]; } } diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs index 7e4ad58..8f9fa64 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs @@ -10,19 +10,19 @@ { Log.Information("Starting ApiWithMutlipleApis"); -var builder = WebApplication.CreateBuilder(args); + var builder = WebApplication.CreateBuilder(args); -builder.Host.UseSerilog((context, loggerConfiguration) => loggerConfiguration - .ReadFrom.Configuration(context.Configuration)); + builder.Host.UseSerilog((context, loggerConfiguration) => loggerConfiguration + .ReadFrom.Configuration(context.Configuration)); -var app = builder - .ConfigureServices() - .ConfigurePipeline(); + var app = builder + .ConfigureServices() + .ConfigurePipeline(); -app.Run(); + app.Run(); } -catch (Exception ex) when(ex.GetType().Name is not "StopTheHostException" +catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" && ex.GetType().Name is not "HostAbortedException") { Log.Fatal(ex, "Unhandled exception"); diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs index 5a9cbcd..cc2f966 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs @@ -36,7 +36,7 @@ public async Task> GetApiDataAsync() var data = await JsonSerializer.DeserializeAsync>( await response.Content.ReadAsStreamAsync()); - if(data != null) + if (data != null) return data; return Array.Empty(); diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/UserApiClientService.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/UserApiClientService.cs index 7b4fedf..784db9b 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/UserApiClientService.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/UserApiClientService.cs @@ -9,9 +9,7 @@ public class UserApiClientService private readonly IHttpClientFactory _clientFactory; private readonly ITokenAcquisition _tokenAcquisition; - public UserApiClientService( - ITokenAcquisition tokenAcquisition, - IHttpClientFactory clientFactory) + public UserApiClientService(ITokenAcquisition tokenAcquisition, IHttpClientFactory clientFactory) { _clientFactory = clientFactory; _tokenAcquisition = tokenAcquisition; @@ -19,7 +17,6 @@ public UserApiClientService( public async Task> GetApiDataAsync() { - var client = _clientFactory.CreateClient(); var scopes = new List { "api://b2a09168-54e2-4bc4-af92-a710a64ef1fa/access_as_user" }; @@ -37,10 +34,9 @@ public async Task> GetApiDataAsync() var data = await JsonSerializer.DeserializeAsync>(stream); - if (data != null) - return data; + if (data != null) return data; - return Array.Empty(); + return []; } throw new ApplicationException("oh no..."); diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/HostingExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs similarity index 98% rename from AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/HostingExtensions.cs rename to AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs index b6f631a..0dc4416 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/HostingExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs @@ -9,7 +9,7 @@ namespace ApiWithMutlipleApis; -internal static class HostingExtensions +internal static class StartupExtensions { public static WebApplication ConfigureServices(this WebApplicationBuilder builder) { @@ -89,7 +89,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde return builder.Build(); } - + public static WebApplication ConfigurePipeline(this WebApplication app) { IdentityModelEventSource.ShowPII = true; diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Controllers/ApiForServiceDataController.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Controllers/ApiForServiceDataController.cs index 8014f9b..9b2f3ec 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Controllers/ApiForServiceDataController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Controllers/ApiForServiceDataController.cs @@ -1,9 +1,9 @@ -using System.Collections.Generic; -using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; +using System.Collections.Generic; namespace ServiceApi.Controllers; diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs index c5179cc..62fc157 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs @@ -85,7 +85,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde return builder.Build(); } - + public static WebApplication ConfigurePipeline(this WebApplication app) { IdentityModelEventSource.ShowPII = true; diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs index e3839fb..2a6bfb4 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ServiceApi; - + public class HasServiceApiRoleHandler : AuthorizationHandler { protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasServiceApiRoleRequirement requirement) diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs index 773fa7f..828d5e5 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs @@ -1,9 +1,9 @@ -using ServiceApi; using Azure.Identity; -using Serilog; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Serilog; +using ServiceApi; using System; Log.Logger = new LoggerConfiguration() diff --git a/AngularMicrosoftEntraIDMultipleApis/UserApi/Controllers/ApiForUserDataController.cs b/AngularMicrosoftEntraIDMultipleApis/UserApi/Controllers/ApiForUserDataController.cs index cd13850..2a3138d 100644 --- a/AngularMicrosoftEntraIDMultipleApis/UserApi/Controllers/ApiForUserDataController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/UserApi/Controllers/ApiForUserDataController.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -namespace UserApiOne.Controllers; +namespace UserApi.Controllers; [Authorize(Policy = "ValidateAccessTokenPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ApiController] diff --git a/AngularMicrosoftEntraIDMultipleApis/UserApi/Program.cs b/AngularMicrosoftEntraIDMultipleApis/UserApi/Program.cs index 6fb33e3..5e2ab96 100644 --- a/AngularMicrosoftEntraIDMultipleApis/UserApi/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/UserApi/Program.cs @@ -77,7 +77,6 @@ JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); IdentityModelEventSource.ShowPII = true; -JwtSecurityTokenHandler.DefaultMapInboundClaims = false; if (app.Environment.IsDevelopment()) { From 68ebb48ed67d7958b16bc2b7fbef8789cabdc29e Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:29:58 +0100 Subject: [PATCH 06/16] git ignore --- .gitignore | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index b0b6a46..0c76ac0 100644 --- a/.gitignore +++ b/.gitignore @@ -8,15 +8,7 @@ **/node_modules/ **/dist/ **/*.csproj.user -/AngularAzureADMultipleApis/LogsServiceApi.txt -/BlazorWithApis/LogsServiceApi.txt -/ConfidentialClientCredentialsCertificate/LogsServiceApi.txt -/ConfidentialClientCredentialsCertificate/LogsMyServerRenderedPortal.txt -/DecryptionCertificates/LogsApiDecryptionCertificates.txt -/DecryptionCertificates/LogsPortalDecryptionCertificates.txt /AngularAzureADMultipleApis/AngularAzureAD/debug.log -/ClientCredentialsFlows/LogsServiceApi.txt -/AngularAzureADMultipleApis/_logs-ApiWithMutlipleApis.txt -/AngularAzureADMultipleApis/_logs-ServiceApi.txt -/ConfidentialClientCredentialsCertificate/_logs-MyServerRenderedPortal.txt -/ConfidentialClientCredentialsCertificate/_logs-ServiceApi.txt +**/_logs* +**/Logs* +**/*.user From be4005102e12d054510d36b22c512c441ce03eda Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:40:21 +0100 Subject: [PATCH 07/16] code clean up --- .../Controllers/DelegatedUserApiCallsController.cs | 4 ++-- .../ApiWithMutlipleApis/Controllers/DirectApiController.cs | 4 ++-- .../Controllers/ServiceApiCallsController.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DelegatedUserApiCallsController.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DelegatedUserApiCallsController.cs index 7b2838e..243c19b 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DelegatedUserApiCallsController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DelegatedUserApiCallsController.cs @@ -7,12 +7,12 @@ namespace ApiWithMutlipleApis.Controllers; [Authorize(Policy = "ValidateAccessTokenPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -[AuthorizeForScopes(Scopes = new string[] { "api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user" })] +[AuthorizeForScopes(Scopes = ["api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user"])] [ApiController] [Route("[controller]")] public class DelegatedUserApiCallsController : ControllerBase { - private UserApiClientService _userApiClientService; + private readonly UserApiClientService _userApiClientService; public DelegatedUserApiCallsController(UserApiClientService userApiClientService) { diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs index 7f0350f..bd162fa 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs @@ -7,7 +7,7 @@ namespace ApiWithMutlipleApis.Controllers; [Authorize(Policy = "ValidateAccessTokenPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -[AuthorizeForScopes(Scopes = new string[] { "api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user" })] +[AuthorizeForScopes(Scopes = ["api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user"])] [ApiController] [Route("[controller]")] public class DirectApiController : ControllerBase @@ -15,6 +15,6 @@ public class DirectApiController : ControllerBase [HttpGet] public IEnumerable Get() { - return new List { "some data", "more data", "loads of data" }; + return ["some data", "more data", "loads of data"]; } } diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/ServiceApiCallsController.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/ServiceApiCallsController.cs index 605b627..77b8a74 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/ServiceApiCallsController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/ServiceApiCallsController.cs @@ -7,12 +7,12 @@ namespace ApiWithMutlipleApis.Controllers; [Authorize(Policy = "ValidateAccessTokenPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] -[AuthorizeForScopes(Scopes = new string[] { "api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user" })] +[AuthorizeForScopes(Scopes = ["api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user"])] [ApiController] [Route("[controller]")] public class ServiceApiCallsController : ControllerBase { - private ServiceApiClientService _serviceApiClientService; + private readonly ServiceApiClientService _serviceApiClientService; public ServiceApiCallsController(ServiceApiClientService serviceApiClientService) { From b70dd850d4c5a663aea6999492ad4831660372c5 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:52:56 +0100 Subject: [PATCH 08/16] clean up --- .../ApiWithMutlipleApis/Controllers/DirectApiController.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs index bd162fa..5407ae7 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Controllers/DirectApiController.cs @@ -5,8 +5,7 @@ namespace ApiWithMutlipleApis.Controllers; -[Authorize(Policy = "ValidateAccessTokenPolicy", - AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] +[Authorize(Policy = "ValidateAccessTokenPolicy", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [AuthorizeForScopes(Scopes = ["api://2b50a014-f353-4c10-aace-024f19a55569/access_as_user"])] [ApiController] [Route("[controller]")] From 889cb0ecfc8cb4d346d4eb968d13be2b7f17f788 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:58:02 +0100 Subject: [PATCH 09/16] code clean up --- .../ApiWithMutlipleApis/ApiWithMutlipleApis.csproj | 5 ----- .../ServiceApi/ServiceApi.csproj | 5 ----- 2 files changed, 10 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj index 1ffb387..b0caf7e 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj @@ -18,15 +18,10 @@ - - - - - diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj index a25c47f..959ca17 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj @@ -16,15 +16,10 @@ - - - - - From a81025cbc43fd0392e047ef25ed6e0fcc95d760d Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 09:58:38 +0100 Subject: [PATCH 10/16] code clean up --- .../ApiWithMutlipleApis/Program.cs | 1 - AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs index 8f9fa64..98fc6a9 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs @@ -3,7 +3,6 @@ Log.Logger = new LoggerConfiguration() .WriteTo.Console() - .WriteTo.AzureApp() .CreateBootstrapLogger(); try diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs index 828d5e5..d582230 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs @@ -8,7 +8,6 @@ Log.Logger = new LoggerConfiguration() .WriteTo.Console() - .WriteTo.AzureApp() .CreateBootstrapLogger(); try From a78ca69fc68bc997f5eee358f8feba4a7aef892b Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 10:02:48 +0100 Subject: [PATCH 11/16] Code clean up --- .../ApiWithMutlipleApis.csproj | 4 ++++ .../ApiWithMutlipleApis/Program.cs | 11 +++++----- .../ApiWithMutlipleApis/StartupExtensions.cs | 5 +---- .../ApiWithMutlipleApis/appsettings.json | 21 +++++++++++++------ .../ServiceApi/ServiceApi.csproj | 3 +++ .../UserApi/UserApi.csproj | 7 +++++++ 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj index b0caf7e..64b81de 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/ApiWithMutlipleApis.csproj @@ -18,10 +18,14 @@ + + + + diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs index 98fc6a9..03fea1e 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Program.cs @@ -5,14 +5,14 @@ .WriteTo.Console() .CreateBootstrapLogger(); +Log.Information("Starting ApiWithMutlipleApis application"); + try { - Log.Information("Starting ApiWithMutlipleApis"); - var builder = WebApplication.CreateBuilder(args); - builder.Host.UseSerilog((context, loggerConfiguration) => loggerConfiguration + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}") .ReadFrom.Configuration(context.Configuration)); var app = builder @@ -21,8 +21,7 @@ app.Run(); } -catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" - && ex.GetType().Name is not "HostAbortedException") +catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" && ex.GetType().Name is not "HostAbortedException") { Log.Fatal(ex, "Unhandled exception"); } @@ -30,4 +29,4 @@ { Log.Information("Shut down complete"); Log.CloseAndFlush(); -} +} \ No newline at end of file diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs index 0dc4416..7d2c5e5 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs @@ -81,7 +81,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme); c.AddSecurityRequirement(new OpenApiSecurityRequirement { - {securityScheme, new string[] { }} + {securityScheme, Array.Empty()} }); c.SwaggerDoc("v1", new OpenApiInfo { Title = "ApiWithMutlipleApis", Version = "v1" }); @@ -105,12 +105,9 @@ public static WebApplication ConfigurePipeline(this WebApplication app) app.UseCors("AllowAllOrigins"); app.UseHttpsRedirection(); - app.UseRouting(); - app.UseAuthentication(); app.UseAuthorization(); - app.MapControllers(); return app; diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/appsettings.json b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/appsettings.json index 56e184e..5c8db09 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/appsettings.json +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/appsettings.json @@ -5,29 +5,38 @@ "TenantId": "7ff95b15-dc21-4ba6-bc92-824856578fc1", "ClientId": "2b50a014-f353-4c10-aace-024f19a55569" }, + //"Serilog": { + // "MinimumLevel": { + // "Default": "Debug", + // "Override": { + // "Microsoft": "Debug", + // "Microsoft.Hosting.Lifetime": "Information", + // "Microsoft.AspNetCore.Authentication": "Debug", + // "System": "Debug" + // } + // } + //}, "Serilog": { - "Using": [ "Serilog.Sinks.Console" ], + "Using": [ "Serilog.Sinks.ApplicationInsights" ], "MinimumLevel": { "Default": "Debug", "Override": { "Microsoft": "Debug", - "Microsoft.EntityFrameworkCore": "Warning", "System": "Debug" } }, "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "WriteTo": [ { - "Name": "Console", + "Name": "ApplicationInsights", "Args": { - "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console", - "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} ({SourceContext}){NewLine}{Exception}" + "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights" } }, { "Name": "File", "Args": { - "path": "../_logs-ApiWithMutlipleApis.txt", + "path": "../../LogFiles/_logs-ApiWithMutlipleApis.txt", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}", "rollOnFileSizeLimit": true, "fileSizeLimitBytes": 4194304, diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj index 959ca17..5c430e3 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/ServiceApi.csproj @@ -16,10 +16,13 @@ + + + diff --git a/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj b/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj index 4665d40..625492f 100644 --- a/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj +++ b/AngularMicrosoftEntraIDMultipleApis/UserApi/UserApi.csproj @@ -10,6 +10,13 @@ + + + + + + + From 610facc8a0878cda128e96bb4b65bdfdeae52e76 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 10:07:19 +0100 Subject: [PATCH 12/16] Code clean up --- .../ServiceApi/Program.cs | 34 ++++--------------- ...tingExtensions.cs => StartupExtensions.cs} | 2 +- .../ServiceApi/appsettings.json | 21 ++++++++---- 3 files changed, 22 insertions(+), 35 deletions(-) rename AngularMicrosoftEntraIDMultipleApis/ServiceApi/{HostingExtensions.cs => StartupExtensions.cs} (99%) diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs index d582230..af80339 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs @@ -1,41 +1,20 @@ -using Azure.Identity; +using ServiceApi; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Serilog; -using ServiceApi; using System; Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateBootstrapLogger(); +Log.Information("Starting ServiceApi application"); + try { - Log.Information("Starting WebApi"); - var builder = WebApplication.CreateBuilder(args); - builder.WebHost - .ConfigureKestrel(serverOptions => { serverOptions.AddServerHeader = false; }) - .ConfigureAppConfiguration((context, configurationBuilder) => - { - var config = configurationBuilder.Build(); - var azureKeyVaultEndpoint = config["AzureKeyVaultEndpoint"]; - if (!string.IsNullOrEmpty(azureKeyVaultEndpoint)) - { - // Add Secrets from KeyVault - Log.Information("Use secrets from {AzureKeyVaultEndpoint}", azureKeyVaultEndpoint); - configurationBuilder.AddAzureKeyVault(new Uri(azureKeyVaultEndpoint), new DefaultAzureCredential()); - } - else - { - // Add Secrets from UserSecrets for local development - configurationBuilder.AddUserSecrets("9f17b08c-435a-4f50-ba7a-802e68ca8d80"); - } - }); - builder.Host.UseSerilog((context, loggerConfiguration) => loggerConfiguration + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}") .ReadFrom.Configuration(context.Configuration)); var app = builder @@ -44,8 +23,7 @@ app.Run(); } -catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" - && ex.GetType().Name is not "HostAbortedException") +catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" && ex.GetType().Name is not "HostAbortedException") { Log.Fatal(ex, "Unhandled exception"); } @@ -53,4 +31,4 @@ { Log.Information("Shut down complete"); Log.CloseAndFlush(); -} +} \ No newline at end of file diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs similarity index 99% rename from AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs rename to AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs index 62fc157..c1a1d83 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/HostingExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs @@ -12,7 +12,7 @@ namespace ServiceApi; -internal static class HostingExtensions +internal static class StartupExtensions { public static WebApplication ConfigureServices(this WebApplicationBuilder builder) { diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/appsettings.json b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/appsettings.json index a1030f4..ac2bb76 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/appsettings.json +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/appsettings.json @@ -5,29 +5,38 @@ "TenantId": "7ff95b15-dc21-4ba6-bc92-824856578fc1", "ClientId": "b178f3a5-7588-492a-924f-72d7887b7e48" }, + //"Serilog": { + // "MinimumLevel": { + // "Default": "Debug", + // "Override": { + // "Microsoft": "Debug", + // "Microsoft.Hosting.Lifetime": "Information", + // "Microsoft.AspNetCore.Authentication": "Debug", + // "System": "Debug" + // } + // } + //}, "Serilog": { - "Using": [ "Serilog.Sinks.Console" ], + "Using": [ "Serilog.Sinks.ApplicationInsights" ], "MinimumLevel": { "Default": "Debug", "Override": { "Microsoft": "Debug", - "Microsoft.EntityFrameworkCore": "Warning", "System": "Debug" } }, "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "WriteTo": [ { - "Name": "Console", + "Name": "ApplicationInsights", "Args": { - "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console", - "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} ({SourceContext}){NewLine}{Exception}" + "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights" } }, { "Name": "File", "Args": { - "path": "../_logs-ServiceApi.txt", + "path": "../../LogFiles/_logs-ServiceApi.txt", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}", "rollOnFileSizeLimit": true, "fileSizeLimitBytes": 4194304, From 4aeb2f9f58b0f937f8060a970ecc73232dc5eff3 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 10:28:34 +0100 Subject: [PATCH 13/16] new certs --- .../certs/dev_localhost.key | 22 +++++++++--------- .../certs/dev_localhost.pem | 22 +++++++++--------- .../certs/dev_localhost.pfx | Bin 1734 -> 1734 bytes 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.key b/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.key index cc4f625..df045f2 100644 --- a/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.key +++ b/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.key @@ -1,13 +1,13 @@ -----BEGIN RSA PRIVATE KEY----- -MIICXQIBAAKBgQCs7HI0KWqXEjH1fxXdkgVH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d8xvCGsnJ+7blaTxXPFpYKSdL +mAxgd2dZcltt9tUTDeMG2VroAiwHZR6ABJDbwmIaStW1JBjtu2u/LML1gjk+JM0CQQD97fRY4v/R +GCwFxZG9NkSUCRkCM4SpxeryChGwMymVoF/ugRhvw4VmR8qCFP8Set+KieXVZpeTGca/dmOtoBov +AkEA5Rp7ZiPp1zAyqWcqzeWAkfVPK9Cmfugs4tKBQkxHtdcb0OdObPd964HT1vH0lJ4eR/XhafWY +5q8rnwQAmyf04wJBAMNCpmFvdJ15w+hQS0g/Ia7f7A4oRXwzR/4ExiNCxFhdcUc6l+rhY4QJ8mvi +711rhNfIQCa/9BBWguEZjX6mnVsCQCaZuaTR9Do/cqm8KigavPjangZB12uWbsmgsgqbk5F9tsAR +2NLfOCYP78NNbOoFYhC5ysrxiFuFtBHMuNVrfAUCQQDqhqY1m1hyWXpOneFJbt4wHTun4QhLhJ6N +yijNL2phxhaM6SgT3lsF+lwjD44UkiT2JOnXwaNYEadK7G2aI0bK -----END RSA PRIVATE KEY----- diff --git a/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.pem b/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.pem index f2247c7..207caa6 100644 --- a/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.pem +++ b/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.pem @@ -1,13 +1,13 @@ -----BEGIN CERTIFICATE----- -MIICQDCCAamgAwIBAgIJAIKGapdMCt4NMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNVBAMTCWxvY2Fs -aG9zdDAeFw0yMDAyMjAyMjU3MjFaFw0zMDAyMjEyMjU3MjFaMBQxEjAQBgNVBAMTCWxvY2FsaG9z -dDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEArOxyNClqlxIx9X8V3ZIFR4PtVIG7QYcJHmd1 -oQb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BgEFBQcDBAYIKwYBBQUH +AwgwHQYDVR0OBBYEFInWKVCkc0wgw1x5XUBvFbFcbDPpMA0GCSqGSIb3DQEBCwUAA4GBADhiql/N +FTLa5oQCWt1Ot6xdvTXHEMf9f6okZkpcZ310IJO8OuptMKW+l+NvJNrpB20ef0XatzAxUVUtTdvz +UfbMQTu541Rf8/bdloiPS2ld8TZNQk4xe5kGbX9EegQ1QVseEhba5lQvQvBhefk4zDnK0Ve7TmY7 +5geNTUjDEcgC -----END CERTIFICATE----- diff --git a/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.pfx b/AngularMicrosoftEntraIDMultipleApis/AngularCliMicrosoftEntraID/certs/dev_localhost.pfx index 737693deb5c51a24a4d62ac6425f2672b416584c..89532c1881a804d1a003a3400b84d688261add5d 100644 GIT binary patch delta 1476 zcmV;#1v~o24aN2Iv<$kc&{K4=6u7FUZ=_Sj!;S8 zc5^vxsk@ZBFI16wQ65RjCiBjB9lE|e?6$l0X3pLCe3lGDAAj3>gL9pq(BsMUXqx~o zbFJVGdL%$&~Xv)^I$t;kC&CL0=+F|USdi^p9``GcnIpLgIi6`e- zY2IcHBm+er3>tP7t_eKjqPMRi1)ei+`@Fs4tvOQrjbnGdOOQA=@$n>$517ro!89{n z8IEAVb^gatp?_=$HU^gReBHoF>UA#;+4MBM8efTBt&XQT;UA} zNQs8D3x(W5fLc5qc%Rl+@Cp9&ptpq7bHitiWsn>RPv*Klts|GQ4<1{~J?-0czz><^3`;^R?ZJ9RC!MX>=x7CdawIx1p(S2W!4gs_a(r z=J~SDnE?oVz1W}$2;a==;|$f^QPHODr=y?dTWI)u!i|AnIUcQN9!|LXCGeBgz*UiH zuk@N9cYhL^OvWQZ3Lv8QGNEhG*DN;!wv1wpJ3SP`7}|T{r}_901ADH8ZkIzy=P}mS zBjFqWG3{;VtFdfMttni5>ZNDq-IfLTjL|yqf_VDqM;JF}hw7X`6-i?}7SSN@3n|g%>dlb-W9L`Hy>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(|~2a+^w*b%{fCq7vKJSW+^6H0$+{1vJaLU;loCC;Oo?Md6)H>PTnlkDr0 z_yVzi|432npnpabiE1HL{Tt5@poxFUrRU)bW+kvLncIko(4?Q4uIRF1agjHy)*hi7s2jQ zFvbMZt=3EK@@VwJBTIU8B$nfeEPj(WhoD1$GM+6co82}n4%HC3#tAgDTyePh>S1TF zC?4|?(}YaD>;XGR)q@txc$_iR;wb{8r()9dU!JBC7GI~i<2`6}rX%5B>ElAfIN6K8 zy>SXr?{1kyEz9#plht7>$eOy!ha3a)?~b8sJGhT1etZN<*Rv*t^j>#ZIrJ!h z^KN%HMo$5um0C47-U;h8s9V@*<5GCvtZ?WL-@KDA$Ec3{{dQ-y22fiMKGnCY=+Wo8-R97V%TVeD*PPkR^EI~rW^ z3u5ErwU&cR3X0zbX;c%Uxk z2*FJYEx7aqDLib6x4k$k*0+;##xHHCkYvm4W2UtMxdg9tjvUS z=cX0#+)Pw1-SAr`dt{n_`W7DwqAi02gNe)3WR?pTXy0NWV(|kMpuyg6bXr0!1)iv3 zqd|4VD_cK%#45x-KD1b=ItOB)O~u5GG$izsz$;woN?O!=gwOX{? z-k$*(;7vw}<8U e;{+6j7-yjG-XZcRD({h!KiIzDV*Dop0te8&Q_p1p From 696b3d7aacc05f9d2c31afb7151edc177e52246b Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 13:48:09 +0100 Subject: [PATCH 14/16] Code clean up --- .../Services/ServiceApiClientService.cs | 7 ++----- AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs index cc2f966..44a6a63 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/Services/ServiceApiClientService.cs @@ -9,8 +9,7 @@ public class ServiceApiClientService private readonly IHttpClientFactory _clientFactory; private readonly ITokenAcquisition _tokenAcquisition; - public ServiceApiClientService( - ITokenAcquisition tokenAcquisition, + public ServiceApiClientService(ITokenAcquisition tokenAcquisition, IHttpClientFactory clientFactory) { _clientFactory = clientFactory; @@ -19,12 +18,10 @@ public ServiceApiClientService( public async Task> GetApiDataAsync() { - var client = _clientFactory.CreateClient(); var scope = "api://b178f3a5-7588-492a-924f-72d7887b7e48/.default"; // CC flow access_as_application"; - var accessToken = await _tokenAcquisition.GetAccessTokenForAppAsync(scope) - .ConfigureAwait(false); + var accessToken = await _tokenAcquisition.GetAccessTokenForAppAsync(scope); client.BaseAddress = new Uri("https://localhost:44324"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs index af80339..2872e29 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Program.cs @@ -1,6 +1,6 @@ -using ServiceApi; using Microsoft.AspNetCore.Builder; using Serilog; +using ServiceApi; using System; Log.Logger = new LoggerConfiguration() From 2e70670432d05c1e66c2f5982ff1750afb8e5685 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 14:01:07 +0100 Subject: [PATCH 15/16] Add claim fallback --- .../ApiWithMutlipleApis/StartupExtensions.cs | 2 +- .../ServiceApi/Policies/HasServiceApiRoleHandler.cs | 6 ++++++ .../ServiceApi/StartupExtensions.cs | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs index 7d2c5e5..2e68a78 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs @@ -93,7 +93,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde public static WebApplication ConfigurePipeline(this WebApplication app) { IdentityModelEventSource.ShowPII = true; - JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); + JsonWebTokenHandler.DefaultMapInboundClaims = false; app.UseSwagger(); app.UseSwaggerUI(c => diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs index 2a6bfb4..9d60b5d 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/Policies/HasServiceApiRoleHandler.cs @@ -18,6 +18,12 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte var roleClaims = context.User.Claims.Where(t => t.Type == "roles"); + // MS namespace: http://schemas.microsoft.com/ws/2008/06/identity/claims/role + if (!roleClaims.Any()) + { + roleClaims = context.User.Claims.Where(t => t.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"); + } + if (roleClaims != null && HasServiceApiRole(roleClaims)) { context.Succeed(requirement); diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs index c1a1d83..5762dcf 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs @@ -4,11 +4,11 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Identity.Web; +using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Logging; using Microsoft.OpenApi.Models; using Serilog; using System; -using System.IdentityModel.Tokens.Jwt; namespace ServiceApi; @@ -89,7 +89,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde public static WebApplication ConfigurePipeline(this WebApplication app) { IdentityModelEventSource.ShowPII = true; - JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); + JsonWebTokenHandler.DefaultMapInboundClaims = false; app.UseSerilogRequestLogging(); From 6d92458a8e40d99c87262976f15bf55e29a121e8 Mon Sep 17 00:00:00 2001 From: damienbod Date: Thu, 2 Jan 2025 14:05:20 +0100 Subject: [PATCH 16/16] mapping --- .../ApiWithMutlipleApis/StartupExtensions.cs | 2 +- .../ServiceApi/StartupExtensions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs index 2e68a78..7d2c5e5 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ApiWithMutlipleApis/StartupExtensions.cs @@ -93,7 +93,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde public static WebApplication ConfigurePipeline(this WebApplication app) { IdentityModelEventSource.ShowPII = true; - JsonWebTokenHandler.DefaultMapInboundClaims = false; + JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); app.UseSwagger(); app.UseSwaggerUI(c => diff --git a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs index 5762dcf..5243373 100644 --- a/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs +++ b/AngularMicrosoftEntraIDMultipleApis/ServiceApi/StartupExtensions.cs @@ -89,7 +89,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde public static WebApplication ConfigurePipeline(this WebApplication app) { IdentityModelEventSource.ShowPII = true; - JsonWebTokenHandler.DefaultMapInboundClaims = false; + JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); app.UseSerilogRequestLogging();