This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
58 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? 🤔