forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuppressHostPrincipalMessageHandler.cs
More file actions
63 lines (55 loc) · 2.33 KB
/
Copy pathSuppressHostPrincipalMessageHandler.cs
File metadata and controls
63 lines (55 loc) · 2.33 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
// 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.Diagnostics.Contracts;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.Properties;
namespace System.Web.Http.Hosting
{
/// <summary>Represents a message handler that suppresses host authentication results.</summary>
/// <remarks>
/// This message handler sets the current principal to anonymous upon entry. As a result, any authentication
/// performed by the host is ignored. The subsequent pipeline, including <see cref="IAuthenticationFilter"/>s, is
/// then the exclusive authority for authentication.
/// </remarks>
public class SuppressHostPrincipalMessageHandler : DelegatingHandler
{
private static readonly Lazy<IPrincipal> _anonymousPrincipal = new Lazy<IPrincipal>(
() => new ClaimsPrincipal(new ClaimsIdentity()), isThreadSafe: true);
/// <inheritdoc />
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
var previousPrincipal = SetCurrentPrincipal(request, _anonymousPrincipal.Value);
try
{
return await base.SendAsync(request, cancellationToken);
}
finally
{
SetCurrentPrincipal(request, previousPrincipal);
}
}
private static IPrincipal SetCurrentPrincipal(HttpRequestMessage request, IPrincipal principal)
{
Contract.Assert(request != null);
HttpRequestContext requestContext = request.GetRequestContext();
if (requestContext == null)
{
throw new ArgumentException(SRResources.Request_RequestContextMustNotBeNull, "request");
}
var previousPrincipal = requestContext.Principal;
requestContext.Principal = principal;
return previousPrincipal;
}
}
}