-
Notifications
You must be signed in to change notification settings - Fork 604
/
CommandResultExtensions.cs
76 lines (67 loc) · 2.68 KB
/
CommandResultExtensions.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using Sustainsys.Saml2.WebSso;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using System;
using System.Text;
using System.Threading.Tasks;
namespace Sustainsys.Saml2.AspNetCore2
{
static class CommandResultExtensions
{
public static async Task Apply(
this CommandResult commandResult,
HttpContext httpContext,
IDataProtector dataProtector,
string signInScheme,
string signOutScheme)
{
httpContext.Response.StatusCode = (int)commandResult.HttpStatusCode;
if(commandResult.Location != null)
{
httpContext.Response.Headers["Location"] = commandResult.Location.OriginalString;
}
if(!string.IsNullOrEmpty(commandResult.SetCookieName))
{
var cookieData = HttpRequestData.ConvertBinaryData(
dataProtector.Protect(commandResult.GetSerializedRequestState()));
httpContext.Response.Cookies.Append(
commandResult.SetCookieName,
cookieData,
new CookieOptions()
{
HttpOnly = true,
// We are expecting a different site to POST back to us,
// so the ASP.Net Core default of Lax is not appropriate in this case
SameSite = SameSiteMode.None
});
}
foreach(var h in commandResult.Headers)
{
httpContext.Response.Headers.Add(h.Key, h.Value);
}
if(!string.IsNullOrEmpty(commandResult.ClearCookieName))
{
httpContext.Response.Cookies.Delete(commandResult.ClearCookieName);
}
if(!string.IsNullOrEmpty(commandResult.Content))
{
var buffer = Encoding.UTF8.GetBytes(commandResult.Content);
httpContext.Response.ContentType = commandResult.ContentType;
await httpContext.Response.Body.WriteAsync(buffer, 0, buffer.Length);
}
if(commandResult.Principal != null)
{
var authProps = new AuthenticationProperties(commandResult.RelayData)
{
RedirectUri = commandResult.Location.OriginalString
};
await httpContext.SignInAsync(signInScheme, commandResult.Principal, authProps);
}
if(commandResult.TerminateLocalSession)
{
await httpContext.SignOutAsync(signOutScheme ?? signInScheme);
}
}
}
}