forked from Sustainsys/Saml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandResultHttpExtensions.cs
56 lines (49 loc) · 2.07 KB
/
CommandResultHttpExtensions.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
using Sustainsys.Saml2.WebSso;
using System;
using System.Net;
using System.Web;
namespace Sustainsys.Saml2.HttpModule
{
public static partial class CommandResultHttpExtensions
{
/// <summary>
/// Apply the command result to a bare HttpResponse.
/// </summary>
/// <param name="commandResult">The CommandResult that will update the HttpResponse.</param>
/// <param name="response">Http Response to write the result to.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "HttpStatusCode")]
public static void Apply(this CommandResult commandResult, HttpResponseBase response)
{
if (commandResult == null)
{
throw new ArgumentNullException(nameof(commandResult));
}
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
response.Cache.SetCacheability((HttpCacheability)commandResult.Cacheability);
ApplyCookies(commandResult, response);
ApplyHeaders(commandResult, response);
if (commandResult.HttpStatusCode == HttpStatusCode.SeeOther || commandResult.Location != null)
{
if (commandResult.Location == null)
{
throw new InvalidOperationException("Missing Location on redirect.");
}
if (commandResult.HttpStatusCode != HttpStatusCode.SeeOther)
{
throw new InvalidOperationException("Invalid HttpStatusCode for redirect, but Location is specified");
}
response.Redirect(commandResult.Location.OriginalString);
}
else
{
response.StatusCode = (int)commandResult.HttpStatusCode;
response.ContentType = commandResult.ContentType;
response.Write(commandResult.Content);
response.End();
}
}
}
}