From 7715b4f92a99c25a638d1371976c5e7b413f8851 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Thu, 16 May 2019 01:22:29 -0700 Subject: [PATCH] Change filter to middleware --- .../Identity/Pages/Account/Logout.cshtml.cs | 1 - src/FrontEnd/Filters/RequireLoginFilter.cs | 43 ----------------- src/FrontEnd/Filters/SkipWelcomeAttribute.cs | 11 ----- .../Middleware/RequireLoginMiddleware.cs | 46 +++++++++++++++++++ .../Middleware/SkipWelcomeAttribute.cs | 10 ++++ src/FrontEnd/Pages/Welcome.cshtml.cs | 1 - src/FrontEnd/Startup.cs | 8 +--- 7 files changed, 58 insertions(+), 62 deletions(-) delete mode 100644 src/FrontEnd/Filters/RequireLoginFilter.cs delete mode 100644 src/FrontEnd/Filters/SkipWelcomeAttribute.cs create mode 100644 src/FrontEnd/Middleware/RequireLoginMiddleware.cs create mode 100644 src/FrontEnd/Middleware/SkipWelcomeAttribute.cs diff --git a/src/FrontEnd/Areas/Identity/Pages/Account/Logout.cshtml.cs b/src/FrontEnd/Areas/Identity/Pages/Account/Logout.cshtml.cs index 4937bca9..784e0e91 100644 --- a/src/FrontEnd/Areas/Identity/Pages/Account/Logout.cshtml.cs +++ b/src/FrontEnd/Areas/Identity/Pages/Account/Logout.cshtml.cs @@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -using FrontEnd.Filters; namespace FrontEnd.Areas.Identity.Pages.Account { diff --git a/src/FrontEnd/Filters/RequireLoginFilter.cs b/src/FrontEnd/Filters/RequireLoginFilter.cs deleted file mode 100644 index 587fee04..00000000 --- a/src/FrontEnd/Filters/RequireLoginFilter.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Linq; -using System.Security.Claims; -using System.Threading.Tasks; -using FrontEnd.Filters; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.AspNetCore.Mvc.Routing; - -namespace FrontEnd -{ - public class RequireLoginFilter : IAsyncResourceFilter - { - private readonly IUrlHelperFactory _urlHelperFactory; - - public RequireLoginFilter(IUrlHelperFactory urlHelperFactory) - { - _urlHelperFactory = urlHelperFactory; - } - - public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) - { - var urlHelper = _urlHelperFactory.GetUrlHelper(context); - - // If the user is authenticated but not a known attendee *and* we've not marked this page - // to skip attendee welcome, then redirect to the Welcome page - if (context.HttpContext.User.Identity.IsAuthenticated && - !context.Filters.OfType().Any()) - { - var isAttendee = context.HttpContext.User.IsAttendee(); - - if (!isAttendee) - { - // No attendee registerd for this user - context.HttpContext.Response.Redirect(urlHelper.Page("/Welcome")); - - return; - } - } - - await next(); - } - } -} \ No newline at end of file diff --git a/src/FrontEnd/Filters/SkipWelcomeAttribute.cs b/src/FrontEnd/Filters/SkipWelcomeAttribute.cs deleted file mode 100644 index 876bbb60..00000000 --- a/src/FrontEnd/Filters/SkipWelcomeAttribute.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using Microsoft.AspNetCore.Mvc.Filters; - -namespace FrontEnd.Filters -{ - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] - public class SkipWelcomeAttribute : Attribute, IFilterMetadata - { - - } -} diff --git a/src/FrontEnd/Middleware/RequireLoginMiddleware.cs b/src/FrontEnd/Middleware/RequireLoginMiddleware.cs new file mode 100644 index 00000000..f87daece --- /dev/null +++ b/src/FrontEnd/Middleware/RequireLoginMiddleware.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; + +namespace FrontEnd +{ + public class RequireLoginMiddleware + { + private readonly RequestDelegate _next; + private readonly LinkGenerator _linkGenerator; + + public RequireLoginMiddleware(RequestDelegate next, LinkGenerator linkGenerator) + { + _next = next; + _linkGenerator = linkGenerator; + } + + public Task Invoke(HttpContext context) + { + var endpoint = context.GetEndpoint(); + + // If the user is authenticated but not a known attendee *and* we've not marked this page + // to skip attendee welcome, then redirect to the Welcome page + if (context.User.Identity.IsAuthenticated && + endpoint?.Metadata.GetMetadata() == null) + { + var isAttendee = context.User.IsAttendee(); + + if (!isAttendee) + { + var url = _linkGenerator.GetUriByPage(context, page: "/Welcome"); + // No attendee registerd for this user + context.Response.Redirect(url); + + return Task.CompletedTask; + } + } + + return _next(context); + } + } +} diff --git a/src/FrontEnd/Middleware/SkipWelcomeAttribute.cs b/src/FrontEnd/Middleware/SkipWelcomeAttribute.cs new file mode 100644 index 00000000..371f7286 --- /dev/null +++ b/src/FrontEnd/Middleware/SkipWelcomeAttribute.cs @@ -0,0 +1,10 @@ +using System; + +namespace FrontEnd +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] + public class SkipWelcomeAttribute : Attribute + { + + } +} diff --git a/src/FrontEnd/Pages/Welcome.cshtml.cs b/src/FrontEnd/Pages/Welcome.cshtml.cs index fea4cc4c..f0c3662f 100644 --- a/src/FrontEnd/Pages/Welcome.cshtml.cs +++ b/src/FrontEnd/Pages/Welcome.cshtml.cs @@ -3,7 +3,6 @@ using FrontEnd.Pages.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using FrontEnd.Filters; using System.Net.Http; using System.Security.Claims; using Microsoft.AspNetCore.Authentication; diff --git a/src/FrontEnd/Startup.cs b/src/FrontEnd/Startup.cs index c69019cf..a04bab05 100644 --- a/src/FrontEnd/Startup.cs +++ b/src/FrontEnd/Startup.cs @@ -22,8 +22,6 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { - services.AddTransient(); - services.AddAuthorization(options => { options.AddPolicy("Admin", policy => @@ -42,10 +40,6 @@ public void ConfigureServices(IServiceCollection services) { options.Conventions.AuthorizeFolder("/Admin", "Admin"); }) - .AddMvcOptions(options => - { - options.Filters.AddService(); - }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); services.AddHealthChecks() @@ -78,6 +72,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseAuthentication(); app.UseAuthorization(); + app.UseMiddleware(); + app.UseEndpoints(endpoints => { endpoints.MapRazorPages();