-
Notifications
You must be signed in to change notification settings - Fork 368
/
RequireLoginMiddleware.cs
46 lines (39 loc) · 1.4 KB
/
RequireLoginMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
}
}
}