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

Commit

Permalink
Change filter to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed May 16, 2019
1 parent 4c1899f commit 7715b4f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 62 deletions.
1 change: 0 additions & 1 deletion src/FrontEnd/Areas/Identity/Pages/Account/Logout.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
43 changes: 0 additions & 43 deletions src/FrontEnd/Filters/RequireLoginFilter.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/FrontEnd/Filters/SkipWelcomeAttribute.cs

This file was deleted.

46 changes: 46 additions & 0 deletions src/FrontEnd/Middleware/RequireLoginMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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<SkipWelcomeAttribute>() == 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);

This comment has been minimized.

Copy link
@kjeske

kjeske May 16, 2019

When I saw that line where you are not awaiting the task I recalled this article:
https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#prefer-asyncawait-over-directly-returning-task
So.. await or not await in such cases? 🤔

}
}
}
10 changes: 10 additions & 0 deletions src/FrontEnd/Middleware/SkipWelcomeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace FrontEnd
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class SkipWelcomeAttribute : Attribute
{

}
}
1 change: 0 additions & 1 deletion src/FrontEnd/Pages/Welcome.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions src/FrontEnd/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<RequireLoginFilter>();

services.AddAuthorization(options =>
{
options.AddPolicy("Admin", policy =>
Expand All @@ -42,10 +40,6 @@ public void ConfigureServices(IServiceCollection services)
{
options.Conventions.AuthorizeFolder("/Admin", "Admin");
})
.AddMvcOptions(options =>
{
options.Filters.AddService<RequireLoginFilter>();
})
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

services.AddHealthChecks()
Expand Down Expand Up @@ -78,6 +72,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthentication();
app.UseAuthorization();

app.UseMiddleware<RequireLoginMiddleware>();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
Expand Down

0 comments on commit 7715b4f

Please sign in to comment.