Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5f90b2d

Browse files
committed
Fix hangs when auth scheme is NTLM/Negotiate
1 parent 3242b8d commit 5f90b2d

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/System.Net.HttpListener/src/System/Net/Managed/ListenerAsyncResult.Managed.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ internal void Complete(HttpListenerContext context, bool synch)
112112
_context = context;
113113
lock (_locker)
114114
{
115-
bool selectionFailure = false;
115+
bool authFailure = false;
116116
try
117117
{
118118
context.AuthenticationSchemes = context._listener.SelectAuthenticationScheme(context);
@@ -124,18 +124,26 @@ internal void Complete(HttpListenerContext context, bool synch)
124124
}
125125
catch
126126
{
127-
selectionFailure = true;
127+
authFailure = true;
128128
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
129129
}
130130

131-
if (selectionFailure ||
132-
((context.AuthenticationSchemes == AuthenticationSchemes.Basic || context._listener.AuthenticationSchemes == AuthenticationSchemes.Negotiate) && context.Request.Headers["Authorization"] == null))
131+
if (context.AuthenticationSchemes != AuthenticationSchemes.None &&
132+
(context.AuthenticationSchemes & AuthenticationSchemes.Anonymous) != AuthenticationSchemes.Anonymous &&
133+
(context.AuthenticationSchemes & AuthenticationSchemes.Basic) != AuthenticationSchemes.Basic)
134+
{
135+
authFailure = true;
136+
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
137+
}
138+
else if (context.AuthenticationSchemes == AuthenticationSchemes.Basic && context.Request.Headers["Authorization"] == null)
139+
{
140+
authFailure = true;
141+
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
142+
context.Response.Headers["WWW-Authenticate"] = context.AuthenticationSchemes + " realm=\"" + context._listener.Realm + "\"";
143+
}
144+
145+
if (authFailure)
133146
{
134-
if (!selectionFailure)
135-
{
136-
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
137-
context.Response.Headers["WWW-Authenticate"] = context.AuthenticationSchemes + " realm=\"" + context._listener.Realm + "\"";
138-
}
139147
context.Response.OutputStream.Close();
140148
IAsyncResult ares = context._listener.BeginGetContext(_cb, _state);
141149
_forward = (ListenerAsyncResult)ares;

src/System.Net.HttpListener/tests/AuthenticationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public async Task TestAnonymousAuthenticationWithDelegate()
134134
await ValidateNullUser();
135135
}
136136

137-
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue(20101, TestPlatforms.AnyUnix)]
137+
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [PlatformSpecific(TestPlatforms.Windows, "Managed impl doesn't support NTLM")]
138138
[ActiveIssue(20096)]
139139
public async Task NtlmAuthentication_Conversation_ReturnsExpectedType2Message()
140140
{
@@ -157,7 +157,7 @@ public static IEnumerable<object[]> InvalidNtlmNegotiateAuthentication_TestData(
157157
yield return new object[] { "abcd", HttpStatusCode.BadRequest };
158158
}
159159

160-
[ConditionalTheory(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue(20101, TestPlatforms.AnyUnix)]
160+
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [PlatformSpecific(TestPlatforms.Windows, "Managed impl doesn't support NTLM")]
161161
[ActiveIssue(20096)]
162162
[MemberData(nameof(InvalidNtlmNegotiateAuthentication_TestData))]
163163
public async Task NtlmAuthentication_InvalidRequestHeaders_ReturnsExpectedStatusCode(string header, HttpStatusCode statusCode)
@@ -180,7 +180,7 @@ public async Task NtlmAuthentication_InvalidRequestHeaders_ReturnsExpectedStatus
180180
}
181181
}
182182

183-
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue(20101, TestPlatforms.AnyUnix)]
183+
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [PlatformSpecific(TestPlatforms.Windows, "Managed impl doesn't support Negotiate")]
184184
[ActiveIssue(20096)]
185185
public async Task NegotiateAuthentication_Conversation_ReturnsExpectedType2Message()
186186
{
@@ -195,7 +195,7 @@ public async Task NegotiateAuthentication_Conversation_ReturnsExpectedType2Messa
195195
}
196196
}
197197

198-
[ConditionalTheory(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue(20101, TestPlatforms.AnyUnix)]
198+
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [PlatformSpecific(TestPlatforms.Windows, "Managed impl doesn't support Negotiate")]
199199
[ActiveIssue(20096)]
200200
[MemberData(nameof(InvalidNtlmNegotiateAuthentication_TestData))]
201201
public async Task NegotiateAuthentication_InvalidRequestHeaders_ReturnsExpectedStatusCode(string header, HttpStatusCode statusCode)

0 commit comments

Comments
 (0)