Skip to content

Commit 89ee34e

Browse files
author
Henrik Zetterström
committed
Replaced the ForceUpdateActionFilter with ForceUpdateMiddleware
1 parent f568119 commit 89ee34e

File tree

8 files changed

+39
-34
lines changed

8 files changed

+39
-34
lines changed

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Boilerplate.Server.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<Using Include="Boilerplate.Shared.Attributes" />
7676
<Using Include="Boilerplate.Server.Api.Data" />
7777
<Using Include="Boilerplate.Server.Api.Mappers" />
78-
<Using Include="Boilerplate.Server.Api.Filters" />
78+
<Using Include="Boilerplate.Server.Api.RequestPipeline" />
7979
<Using Include="Microsoft.AspNetCore.Identity" />
8080
<Using Include="Microsoft.EntityFrameworkCore" />
8181
<Using Include="Microsoft.AspNetCore.OData.Query" />

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Filters/ForceUpdateActionFilter.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Middlewares.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ private static void ConfigureMiddlewares(this WebApplication app)
4343

4444
app.UseCors();
4545

46+
if (settings.SupportedAppVersions is not null)
47+
{
48+
app.UseMiddleware<ForceUpdateMiddleware>();
49+
}
4650
app.UseAuthentication();
4751
app.UseAuthorization();
4852

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Services.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,7 @@ public static void AddServerApiProjectServices(this WebApplicationBuilder builde
187187
services.AddSingleton<HtmlSanitizer>();
188188

189189
services
190-
.AddControllers(options =>
191-
{
192-
if (appSettings.SupportedAppVersions is not null)
193-
{
194-
options.Filters.Add<ForceUpdateActionFilter>();
195-
}
196-
})
190+
.AddControllers()
197191
.AddJsonOptions(options =>
198192
{
199193
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Boilerplate.Server.Api.RequestPipeline;
2+
3+
public class ForceUpdateMiddleware(RequestDelegate next, ServerApiSettings settings)
4+
{
5+
private readonly RequestDelegate next = next;
6+
private readonly ServerApiSettings settings = settings;
7+
8+
public async Task InvokeAsync(HttpContext context)
9+
{
10+
if (context.Request.Headers.TryGetValue("X-App-Version", out var appVersionHeaderValue)
11+
&& appVersionHeaderValue.Any())
12+
{
13+
var appVersion = appVersionHeaderValue.Single()!;
14+
var appPlatformType = Enum.Parse<AppPlatformType>(context.Request.Headers["X-App-Platform"].Single()!);
15+
var minVersion = settings.SupportedAppVersions!.GetMinimumSupportedAppVersion(appPlatformType);
16+
if (minVersion != null && Version.Parse(appVersion) < minVersion)
17+
{
18+
throw new ClientNotSupportedException();
19+
}
20+
}
21+
22+
await next(context);
23+
}
24+
}

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Filters/HangfireDashboardAuthorizationFilter.cs renamed to src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/RequestPipeline/HangfireDashboardAuthorizationFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Hangfire.Dashboard;
22
using Hangfire.Annotations;
33

4-
namespace Boilerplate.Server.Api.Filters;
4+
namespace Boilerplate.Server.Api.RequestPipeline;
55

66
public class HangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
77
{

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Filters/ODataOperationFilter.cs renamed to src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/RequestPipeline/ODataOperationFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Microsoft.OpenApi.Models;
33
using Swashbuckle.AspNetCore.SwaggerGen;
44

5-
namespace Boilerplate.Server.Api.Filters;
5+
namespace Boilerplate.Server.Api.RequestPipeline;
66

77
/// <summary>
88
/// https://docs.microsoft.com/en-us/odata/concepts/queryoptions-overview

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//#if (api == "Integrated")
1212
using Hangfire;
1313
using Boilerplate.Server.Api;
14-
using Boilerplate.Server.Api.Filters;
14+
using Boilerplate.Server.Api.RequestPipeline;
1515
using Boilerplate.Server.Api.Services;
1616
//#endif
1717

@@ -29,6 +29,8 @@ public static void ConfigureMiddlewares(this WebApplication app)
2929

3030
ServerWebSettings settings = new();
3131
configuration.Bind(settings);
32+
ServerApiSettings apiSettings = new();
33+
configuration.Bind(apiSettings);
3234

3335
app.UseAppForwardedHeaders();
3436

@@ -101,6 +103,10 @@ public static void ConfigureMiddlewares(this WebApplication app)
101103
app.UseCors();
102104
//#endif
103105

106+
if (apiSettings.SupportedAppVersions is not null)
107+
{
108+
app.UseMiddleware<ForceUpdateMiddleware>();
109+
}
104110
app.UseAuthentication();
105111
app.UseAuthorization();
106112

0 commit comments

Comments
 (0)