forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassiveAuthenticationMessageHandler.cs
More file actions
100 lines (84 loc) · 4.07 KB
/
PassiveAuthenticationMessageHandler.cs
File metadata and controls
100 lines (84 loc) · 4.07 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
// 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.Owin.Properties;
using Microsoft.Owin.Security;
namespace System.Web.Http.Owin
{
/// <summary>Represents a message handler that treats all OWIN authentication middleware as passive.</summary>
/// <remarks>
/// This message handler sets the current principal to anonymous upon entry and disables the default OWIN
/// authentication middleware challenges. As a result, any default 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 PassiveAuthenticationMessageHandler : 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");
}
HttpResponseMessage response;
var previousPrincipal = SetCurrentPrincipal(request, _anonymousPrincipal.Value);
try
{
response = await base.SendAsync(request, cancellationToken);
}
finally
{
SetCurrentPrincipal(request, previousPrincipal);
}
SuppressDefaultAuthenticationChallenges(request);
return response;
}
private static IPrincipal SetCurrentPrincipal(HttpRequestMessage request, IPrincipal principal)
{
Contract.Assert(request != null);
HttpRequestContext requestContext = request.GetRequestContext();
if (requestContext == null)
{
throw new ArgumentException(OwinResources.Request_RequestContextMustNotBeNull, "request");
}
var previousPrincipal = requestContext.Principal;
requestContext.Principal = principal;
return previousPrincipal;
}
private static void SuppressDefaultAuthenticationChallenges(HttpRequestMessage request)
{
Contract.Assert(request != null);
IAuthenticationManager authenticationManager = request.GetAuthenticationManager();
if (authenticationManager == null)
{
throw new InvalidOperationException(OwinResources.IAuthenticationManagerNotAvailable);
}
AuthenticationResponseChallenge currentChallenge = authenticationManager.AuthenticationResponseChallenge;
// A null challenge or challenge.AuthenticationTypes == null or empty represents the the default behavior
// of running all active authentication middleware challenges.
// Provide an array with a single null item to suppress this default behavior.
string[] suppressAuthenticationTypes = new string[] { null };
if (currentChallenge == null)
{
authenticationManager.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(
suppressAuthenticationTypes, new AuthenticationProperties());
}
else if (currentChallenge.AuthenticationTypes == null || currentChallenge.AuthenticationTypes.Length == 0)
{
authenticationManager.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(
suppressAuthenticationTypes, currentChallenge.Properties);
}
}
}
}