Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ForceManagedImplementation>false</ForceManagedImplementation>
Expand All @@ -9,6 +9,7 @@
<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == ''">SR.SystemNetHttpListener_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
<DefineConstants Condition="'$(TargetPlatformIdentifier)' == 'windows'">$(DefineConstants);TARGET_WINDOWS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Principal.Windows\src\System.Security.Principal.Windows.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ internal HttpListenerWebSocketContext(
else
{
// AuthenticationSchemes.Digest, AuthenticationSchemes.Negotiate, AuthenticationSchemes.NTLM.
#if TARGET_WINDOWS
WindowsIdentity windowsIdentity = (WindowsIdentity)user.Identity!;
return new WindowsPrincipal(new WindowsIdentity(windowsIdentity.Token, windowsIdentity.AuthenticationType!, WindowsAccountType.Normal, true));
#else
throw new PlatformNotSupportedException();
#endif

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Net.Tests
Expand All @@ -24,7 +23,7 @@ public class HttpListenerFactory : IDisposable
private readonly string _path;
private readonly int _port;

internal HttpListenerFactory(string hostname = "localhost", string path = null)
internal HttpListenerFactory(string hostname = "localhost", string path = null, AuthenticationSchemes? authenticationSchemes = null)
{
// Find a URL prefix that is not in use on this machine *and* uses a port that's not in use.
// Once we find this prefix, keep a listener on it for the duration of the process, so other processes
Expand All @@ -42,6 +41,12 @@ internal HttpListenerFactory(string hostname = "localhost", string path = null)
try
{
listener.Prefixes.Add(prefix);

if (authenticationSchemes != null)
{
listener.AuthenticationSchemes = authenticationSchemes.Value;
}

listener.Start();

_processPrefixListener = listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class HttpListenerWebSocketTests : IDisposable
{
public static bool IsNotWindows7 { get; } = !PlatformDetection.IsWindows7;
public static bool IsNotWindows7AndIsWindowsImplementation => IsNotWindows7 && Helpers.IsWindowsImplementation;
public static bool IsWindows8OrLater { get; } = PlatformDetection.IsWindows8xOrLater;

private HttpListenerFactory Factory { get; }
private HttpListener Listener { get; }
Expand Down Expand Up @@ -363,6 +364,41 @@ public async Task Abort_CallAfterAborted_Nop()
Assert.Equal(WebSocketState.Aborted, context.WebSocket.State);
}

[ConditionalFact(nameof(IsWindows8OrLater))]
public async Task ReceiveAsync_ReadBuffer_WithWindowsAuthScheme_Success()
{
HttpListenerFactory factory = new HttpListenerFactory(authenticationSchemes: AuthenticationSchemes.IntegratedWindowsAuthentication);
var uriBuilder = new UriBuilder(factory.ListeningUrl) { Scheme = "ws" };
Task<HttpListenerContext> serverContextTask = factory.GetListener().GetContextAsync();
ClientWebSocket client = new ClientWebSocket();
client.Options.Credentials = CredentialCache.DefaultCredentials;

Task clientConnectTask = client.ConnectAsync(uriBuilder.Uri, CancellationToken.None);
if (clientConnectTask == await Task.WhenAny(serverContextTask, clientConnectTask))
{
await clientConnectTask;
Assert.True(false, "Client should not have completed prior to server sending response");
}

HttpListenerContext context = await serverContextTask;
HttpListenerWebSocketContext wsContext = await context.AcceptWebSocketAsync(null);
await clientConnectTask;

const string Text = "Hello Web Socket";
byte[] sentBytes = Encoding.ASCII.GetBytes(Text);

await client.SendAsync(new ArraySegment<byte>(sentBytes), WebSocketMessageType.Text, true, new CancellationToken());

byte[] receivedBytes = new byte[sentBytes.Length];
WebSocketReceiveResult result = await ReceiveAllAsync(wsContext.WebSocket, receivedBytes.Length, receivedBytes);
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
Assert.True(result.EndOfMessage);
Assert.Null(result.CloseStatus);
Assert.Null(result.CloseStatusDescription);

Assert.Equal(Text, Encoding.ASCII.GetString(receivedBytes));
}

private static async Task<WebSocketReceiveResult> ReceiveAllAsync(WebSocket webSocket, int expectedBytes, byte[] buffer)
{
int totalReceived = 0;
Expand Down