-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathStrictSameSiteExternalAuthenticationMiddleware.cs
More file actions
62 lines (54 loc) · 2.29 KB
/
StrictSameSiteExternalAuthenticationMiddleware.cs
File metadata and controls
62 lines (54 loc) · 2.29 KB
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
52
53
54
55
56
57
58
59
60
61
62
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
namespace IdentityModel.AspNetCore
{
// Fixes for Safari
// https://brockallen.com/2019/01/11/same-site-cookies-asp-net-core-and-external-authentication-providers/
public class StrictSameSiteExternalAuthenticationMiddleware
{
private readonly RequestDelegate _next;
public StrictSameSiteExternalAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext ctx)
{
var schemes = ctx.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>();
var handlers = ctx.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
foreach (var scheme in await schemes.GetRequestHandlerSchemesAsync())
{
var handler = await handlers.GetHandlerAsync(ctx, scheme.Name) as IAuthenticationRequestHandler;
if (handler != null && await handler.HandleRequestAsync())
{
// start same-site cookie special handling
string location = null;
if (ctx.Response.StatusCode == 302)
{
location = ctx.Response.Headers["location"];
}
else if (ctx.Request.Method == "GET" && !ctx.Request.Query["skip"].Any())
{
location = ctx.Request.Path + ctx.Request.QueryString + "&skip=1";
}
if (location != null)
{
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = MediaTypeNames.Text.Html;
var html = $@"
<html><head>
<meta http-equiv='refresh' content='0;url={location}' />
</head></html>";
await ctx.Response.WriteAsync(html);
}
// end same-site cookie special handling
return;
}
}
await _next(ctx);
}
}
}