Skip to content

Commit ba254fb

Browse files
committed
Merge branch 'dev'
2 parents 2f193cb + d0c2576 commit ba254fb

File tree

13 files changed

+156
-133
lines changed

13 files changed

+156
-133
lines changed

BlazorWithApis/BlazorAzureADWithApis/Client/BlazorAzureADWithApis.Client.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0" PrivateAssets="all" />
13-
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="9.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
15-
<PackageReference Include="System.Net.Http.Json" Version="9.0.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.1" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="10.0.1" PrivateAssets="all" />
13+
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="10.0.1" />
14+
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.1" />
15+
<PackageReference Include="System.Net.Http.Json" Version="10.0.1" />
1616
</ItemGroup>
1717

1818
<ItemGroup>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<UserSecretsId>3c83752e-aded-433a-8d36-41e450f542f2</UserSecretsId>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
@@ -13,10 +13,10 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.0" />
17-
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraphBeta" Version="3.5.0" />
18-
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="9.0.0" NoWarn="NU1605" />
19-
<PackageReference Include="Microsoft.Identity.Web" Version="3.5.0" />
16+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="10.0.1" />
17+
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraphBeta" Version="4.1.1" />
18+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="10.0.1" NoWarn="NU1605" />
19+
<PackageReference Include="Microsoft.Identity.Web" Version="4.1.1" />
2020
</ItemGroup>
2121

2222
</Project>

BlazorWithApis/BlazorAzureADWithApis/Shared/BlazorAzureADWithApis.Shared.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.OpenApi;
3+
using Microsoft.OpenApi;
4+
5+
namespace ServiceApi;
6+
7+
internal sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider) : IOpenApiDocumentTransformer
8+
{
9+
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
10+
{
11+
var authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
12+
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
13+
{
14+
var requirements = new Dictionary<string, IOpenApiSecurityScheme>
15+
{
16+
["Bearer"] = new OpenApiSecurityScheme
17+
{
18+
Type = SecuritySchemeType.Http,
19+
Scheme = "bearer", // "bearer" refers to the header name here
20+
In = ParameterLocation.Header,
21+
BearerFormat = "Json Web Token"
22+
}
23+
};
24+
document.Components ??= new OpenApiComponents();
25+
document.Components.SecuritySchemes = requirements;
26+
}
27+
document.Info = new()
28+
{
29+
Title = "My API Bearer scheme",
30+
Version = "v1",
31+
Description = "API for Damien"
32+
};
33+
}
34+
}

BlazorWithApis/ServiceApi/Controllers/ApiForServiceDataController.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Mvc;
5-
using Swashbuckle.AspNetCore.Annotations;
65
using System.Collections.Generic;
76

87
namespace ServiceApi.Controllers;
@@ -13,14 +12,12 @@ namespace ServiceApi.Controllers;
1312
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
1413
[ProducesResponseType(StatusCodes.Status403Forbidden)]
1514
[Produces("application/json")]
16-
[SwaggerTag("Service API for demo service data")]
1715
public class ApiForServiceDataController : ControllerBase
1816
{
1917
[HttpGet]
2018
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<string>))]
21-
[SwaggerOperation(OperationId = "Get", Summary = "Gets service data")]
2219
public IEnumerable<string> Get()
2320
{
24-
return new List<string> { "app-app Service API data 1", "service API data 2" };
21+
return ["app-app Service API data 1", "service API data 2"];
2522
}
2623
}

BlazorWithApis/ServiceApi/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"ServiceApi": {
44
"commandName": "Project",
55
"launchBrowser": true,
6-
"launchUrl": "",
6+
"launchUrl": "swagger",
77
"environmentVariables": {
88
"ASPNETCORE_ENVIRONMENT": "Development"
99
},
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<UserSecretsId>196b270c-b0c0-4b90-8f04-d3108e701d51</UserSecretsId>
66
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<PackageReference Include="Microsoft.Identity.Web" Version="3.5.0" />
11+
<PackageReference Include="Microsoft.Identity.Web" Version="4.1.1" />
1112

12-
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="1.0.0-preview.1" />
13-
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="1.0.0-preview.1" />
14-
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
15-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />
13+
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="1.3.0" />
14+
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="1.3.0" />
1615

17-
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.2" />
18-
<PackageReference Include="Azure.Identity" Version="1.13.1" />
16+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
17+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.0.1" />
1918

20-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
21-
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
19+
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.4.0" />
20+
<PackageReference Include="Azure.Identity" Version="1.17.1" />
21+
22+
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
2223
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
2324
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
2425
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
25-
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
26+
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.1.0" />
2627
</ItemGroup>
2728

2829
</Project>
Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using Microsoft.AspNetCore.Authentication.JwtBearer;
21
using Microsoft.AspNetCore.Authorization;
32
using Microsoft.AspNetCore.Builder;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Hosting;
65
using Microsoft.Identity.Web;
76
using Microsoft.IdentityModel.JsonWebTokens;
87
using Microsoft.IdentityModel.Logging;
9-
using Microsoft.OpenApi.Models;
108
using NetEscapades.AspNetCore.SecurityHeaders.Infrastructure;
119
using Serilog;
12-
using System;
1310

1411
namespace ServiceApi;
1512

@@ -21,11 +18,11 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
2118
var configuration = builder.Configuration;
2219

2320
services.AddSecurityHeaderPolicies()
24-
.SetPolicySelector((PolicySelectorContext ctx) =>
25-
{
26-
return SecurityHeadersDefinitions.GetHeaderPolicyCollection(
27-
builder.Environment.IsDevelopment());
28-
});
21+
.SetPolicySelector((PolicySelectorContext ctx) =>
22+
{
23+
return SecurityHeadersDefinitions.GetHeaderPolicyCollection(
24+
builder.Environment.IsDevelopment());
25+
});
2926

3027
services.AddSingleton<IAuthorizationHandler, HasServiceApiRoleHandler>();
3128

@@ -51,44 +48,19 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
5148
validateAccessTokenPolicy.RequireClaim("azpacr", "1");
5249
});
5350
});
54-
55-
services.AddSwaggerGen(c =>
51+
services.AddOpenApi(options =>
5652
{
57-
c.EnableAnnotations();
58-
59-
// add JWT Authentication
60-
var securityScheme = new OpenApiSecurityScheme
61-
{
62-
Name = "JWT Authentication",
63-
Description = "Enter JWT Bearer token **_only_**",
64-
In = ParameterLocation.Header,
65-
Type = SecuritySchemeType.Http,
66-
Scheme = "bearer", // must be lower case
67-
BearerFormat = "JWT",
68-
Reference = new OpenApiReference
69-
{
70-
Id = JwtBearerDefaults.AuthenticationScheme,
71-
Type = ReferenceType.SecurityScheme
72-
}
73-
};
74-
c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);
75-
c.AddSecurityRequirement(new OpenApiSecurityRequirement
76-
{
77-
{securityScheme, Array.Empty<string>()}
78-
});
79-
80-
c.SwaggerDoc("v1", new OpenApiInfo
81-
{
82-
Title = "Service API One",
83-
Version = "v1",
84-
Description = "Service API One",
85-
Contact = new OpenApiContact
86-
{
87-
Name = "damienbod",
88-
Email = string.Empty,
89-
Url = new Uri("https://damienbod.com/"),
90-
},
91-
});
53+
//options.UseTransformer((document, context, cancellationToken) =>
54+
//{
55+
// document.Info = new()
56+
// {
57+
// Title = "My API",
58+
// Version = "v1",
59+
// Description = "API for Damien"
60+
// };
61+
// return Task.CompletedTask;
62+
//});
63+
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
9264
});
9365

9466
return builder.Build();
@@ -99,22 +71,15 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
9971
IdentityModelEventSource.ShowPII = true;
10072
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear();
10173

102-
app.UseSerilogRequestLogging();
103-
10474
app.UseSecurityHeaders();
10575

76+
app.UseSerilogRequestLogging();
77+
10678
if (app.Environment.IsDevelopment())
10779
{
10880
app.UseDeveloperExceptionPage();
10981
}
11082

111-
app.UseSwagger();
112-
app.UseSwaggerUI(c =>
113-
{
114-
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Service API One");
115-
c.RoutePrefix = string.Empty;
116-
});
117-
11883
app.UseSerilogRequestLogging();
11984

12085
app.UseHttpsRedirection();
@@ -126,6 +91,16 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
12691

12792
app.MapControllers();
12893

94+
app.MapOpenApi("/openapi/v1/openapi.json");
95+
96+
if (app.Environment.IsDevelopment())
97+
{
98+
app.UseSwaggerUI(options =>
99+
{
100+
options.SwaggerEndpoint("/openapi/v1/openapi.json", "v1");
101+
});
102+
}
103+
129104
return app;
130105
}
131106
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.OpenApi;
3+
using Microsoft.OpenApi;
4+
5+
namespace UserApi;
6+
7+
internal sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider) : IOpenApiDocumentTransformer
8+
{
9+
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
10+
{
11+
var authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
12+
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
13+
{
14+
var requirements = new Dictionary<string, IOpenApiSecurityScheme>
15+
{
16+
["Bearer"] = new OpenApiSecurityScheme
17+
{
18+
Type = SecuritySchemeType.Http,
19+
Scheme = "bearer", // "bearer" refers to the header name here
20+
In = ParameterLocation.Header,
21+
BearerFormat = "Json Web Token"
22+
}
23+
};
24+
document.Components ??= new OpenApiComponents();
25+
document.Components.SecuritySchemes = requirements;
26+
}
27+
document.Info = new()
28+
{
29+
Title = "My API Bearer scheme",
30+
Version = "v1",
31+
Description = "API for Damien"
32+
};
33+
}
34+
}

BlazorWithApis/UserApi/Controllers/ApiForUserDataController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public class ApiForUserDataController : ControllerBase
1212
[HttpGet]
1313
public IEnumerable<string> Get()
1414
{
15-
return new List<string> { "user API data 1", "user API data 2" };
15+
return ["user API data 1", "user API data 2"];
1616
}
1717
}

0 commit comments

Comments
 (0)