-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie-invalidation.txt
52 lines (32 loc) · 1.02 KB
/
cookie-invalidation.txt
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
47
48
49
50
51
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
var builder = WebApplication.CreateBuilder(args);
List<string> blacklist = new();
builder.Services.AddAuthentication("cookie")
.AddCookie("cookie", o =>
{
o.Events.OnValidatePrincipal = ctx =>
{
if(blacklist.Contains(ctx.Principal?.FindFirstValue("session")))
{
ctx.RejectPrincipal();
}
return Task.CompletedTask;
};
});
var app = builder.Build();
app.MapGet("/login", () => Results.SignIn(
new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.NameIdentifier,Guid.NewGuid().ToString()),
new Claim("session",Guid.NewGuid().ToString()),
}, "cookie")),
new AuthenticationProperties(),
"cookie"
));
app.MapGet("/user",(ClaimsPrincipal user)=>user.Claims.Select(x => new {x.Type,x.Value}).ToList());
app.MapGet("/blacklist", (string session) =>
{
blacklist.Add(session);
});
app.Run();