Skip to content

Commit

Permalink
Merge branch 'net9'
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Jan 2, 2025
2 parents 8d1aa17 + 1f3b89d commit cdfb7e9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Threading.Tasks;

namespace ServiceApi;

public class HasServiceApiRoleHandler : AuthorizationHandler<HasServiceApiRoleRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasServiceApiRoleRequirement requirement)
Expand All @@ -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);
Expand Down
37 changes: 7 additions & 30 deletions ClientCredentialsFlows/ServiceApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
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()
.WriteTo.Console()
.WriteTo.AzureApp()
.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
Expand All @@ -45,13 +23,12 @@

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");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev)
{
var policy = new HeaderPolicyCollection()
.AddFrameOptionsDeny()
.AddXssProtectionBlock()
.AddContentTypeOptionsNoSniff()
.AddReferrerPolicyStrictOriginWhenCrossOrigin()
.RemoveServerHeader()
Expand Down
30 changes: 14 additions & 16 deletions ClientCredentialsFlows/ServiceApi/ServiceApi.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<UserSecretsId>196b270c-b0c0-4b90-8f04-d3108e701d51</UserSecretsId>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="2.16.1" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.5.0" />

<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.21.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="0.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.24.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="0.24.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />

<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.0" />
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.3.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.2" />
<PackageReference Include="Azure.Identity" Version="1.13.1" />

<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.AzureApp" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace ServiceApi;

internal static class HostingExtensions
internal static class StartupExtensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
Expand Down Expand Up @@ -85,7 +85,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde

return builder.Build();
}

public static WebApplication ConfigurePipeline(this WebApplication app)
{
IdentityModelEventSource.ShowPII = true;
Expand Down
27 changes: 18 additions & 9 deletions ClientCredentialsFlows/ServiceApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "Information",
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"Microsoft.EntityFrameworkCore": "Warning",
"System": "Information"
"Microsoft": "Debug",
"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": "../LogsServiceApi.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,
Expand Down
2 changes: 2 additions & 0 deletions ClientCredentialsFlows/ServiceApiClientConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}

Console.ReadKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>78cf2604-554c-4a6e-8846-3505f2c0697d</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.58.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.66.2" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit cdfb7e9

Please sign in to comment.