forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwinHttpRequestMessageExtensions.cs
More file actions
105 lines (91 loc) · 4 KB
/
OwinHttpRequestMessageExtensions.cs
File metadata and controls
105 lines (91 loc) · 4 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Owin;
using Microsoft.Owin.Security;
namespace System.Net.Http
{
/// <summary>
/// Provides extension methods for the <see cref="HttpRequestMessage"/> class.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class OwinHttpRequestMessageExtensions
{
private const string OwinEnvironmentKey = "MS_OwinEnvironment";
private const string OwinContextKey = "MS_OwinContext";
/// <summary>Gets the OWIN context for the specified request.</summary>
/// <param name="request">The HTTP request message.</param>
/// <returns>
/// The OWIN environment for the specified context, if available; otherwise <see langword="null"/>.
/// </returns>
public static IOwinContext GetOwinContext(this HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
IOwinContext context;
if (!request.Properties.TryGetValue<IOwinContext>(OwinContextKey, out context))
{
// If the OWIN context is not available, try to create by upgrading an OWIN environment property
// instead.
IDictionary<string, object> environment;
if (request.Properties.TryGetValue<IDictionary<string, object>>(OwinEnvironmentKey, out environment))
{
context = new OwinContext(environment);
SetOwinContext(request, context);
request.Properties.Remove(OwinEnvironmentKey);
}
}
return context;
}
/// <summary>Sets the OWIN context for the specified request.</summary>
/// <param name="request">The HTTP request message.</param>
/// <param name="context">The OWIN context to set.</param>
public static void SetOwinContext(this HttpRequestMessage request, IOwinContext context)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (context == null)
{
throw new ArgumentNullException("context");
}
request.Properties[OwinContextKey] = context;
// Make sure only one of the two properties exists (single source of truth).
request.Properties.Remove(OwinEnvironmentKey);
}
/// <summary>Gets the OWIN environment for the specified request.</summary>
/// <param name="request">The HTTP request message.</param>
/// <returns>
/// The OWIN environment for the specified request, if available; otherwise <see langword="null"/>.
/// </returns>
public static IDictionary<string, object> GetOwinEnvironment(this HttpRequestMessage request)
{
IOwinContext context = GetOwinContext(request);
if (context == null)
{
return null;
}
return context.Environment;
}
/// <summary>Sets the OWIN environment for the specified request.</summary>
/// <param name="request">The HTTP request message.</param>
/// <param name="environment">The OWIN environment to set.</param>
public static void SetOwinEnvironment(this HttpRequestMessage request, IDictionary<string, object> environment)
{
SetOwinContext(request, new OwinContext(environment));
}
internal static IAuthenticationManager GetAuthenticationManager(this HttpRequestMessage request)
{
IOwinContext context = GetOwinContext(request);
if (context == null)
{
return null;
}
return context.Authentication;
}
}
}