Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 7715b4f

Browse files
committed
Change filter to middleware
1 parent 4c1899f commit 7715b4f

File tree

7 files changed

+58
-62
lines changed

7 files changed

+58
-62
lines changed

src/FrontEnd/Areas/Identity/Pages/Account/Logout.cshtml.cs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.AspNetCore.Mvc;
99
using Microsoft.AspNetCore.Mvc.RazorPages;
1010
using Microsoft.Extensions.Logging;
11-
using FrontEnd.Filters;
1211

1312
namespace FrontEnd.Areas.Identity.Pages.Account
1413
{

src/FrontEnd/Filters/RequireLoginFilter.cs

-43
This file was deleted.

src/FrontEnd/Filters/SkipWelcomeAttribute.cs

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Routing;
8+
9+
namespace FrontEnd
10+
{
11+
public class RequireLoginMiddleware
12+
{
13+
private readonly RequestDelegate _next;
14+
private readonly LinkGenerator _linkGenerator;
15+
16+
public RequireLoginMiddleware(RequestDelegate next, LinkGenerator linkGenerator)
17+
{
18+
_next = next;
19+
_linkGenerator = linkGenerator;
20+
}
21+
22+
public Task Invoke(HttpContext context)
23+
{
24+
var endpoint = context.GetEndpoint();
25+
26+
// If the user is authenticated but not a known attendee *and* we've not marked this page
27+
// to skip attendee welcome, then redirect to the Welcome page
28+
if (context.User.Identity.IsAuthenticated &&
29+
endpoint?.Metadata.GetMetadata<SkipWelcomeAttribute>() == null)
30+
{
31+
var isAttendee = context.User.IsAttendee();
32+
33+
if (!isAttendee)
34+
{
35+
var url = _linkGenerator.GetUriByPage(context, page: "/Welcome");
36+
// No attendee registerd for this user
37+
context.Response.Redirect(url);
38+
39+
return Task.CompletedTask;
40+
}
41+
}
42+
43+
return _next(context);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace FrontEnd
4+
{
5+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
6+
public class SkipWelcomeAttribute : Attribute
7+
{
8+
9+
}
10+
}

src/FrontEnd/Pages/Welcome.cshtml.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using FrontEnd.Pages.Models;
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.AspNetCore.Mvc.RazorPages;
6-
using FrontEnd.Filters;
76
using System.Net.Http;
87
using System.Security.Claims;
98
using Microsoft.AspNetCore.Authentication;

src/FrontEnd/Startup.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public Startup(IConfiguration configuration)
2222

2323
public void ConfigureServices(IServiceCollection services)
2424
{
25-
services.AddTransient<RequireLoginFilter>();
26-
2725
services.AddAuthorization(options =>
2826
{
2927
options.AddPolicy("Admin", policy =>
@@ -42,10 +40,6 @@ public void ConfigureServices(IServiceCollection services)
4240
{
4341
options.Conventions.AuthorizeFolder("/Admin", "Admin");
4442
})
45-
.AddMvcOptions(options =>
46-
{
47-
options.Filters.AddService<RequireLoginFilter>();
48-
})
4943
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
5044

5145
services.AddHealthChecks()
@@ -78,6 +72,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7872
app.UseAuthentication();
7973
app.UseAuthorization();
8074

75+
app.UseMiddleware<RequireLoginMiddleware>();
76+
8177
app.UseEndpoints(endpoints =>
8278
{
8379
endpoints.MapRazorPages();

0 commit comments

Comments
 (0)