-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpListenerContext.AcceptWebSocketAsync unsupported for IWindowsPrincipal in .NET Core 3.1 #63738
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionHttpListenerContext.AcceptWebSocketAsync throws PlatformNotSupportedException when called with User set to IWindowsPrincipal. This happens in .NET Core 3.1 when running on Windows. Works fine when running in .NET Framework. Reproduction StepsHttpListenerContext ctx = await listener.GetContextAsync(); Expected behaviorIt should not throw the exception and work the same way as it does in .NET Framework Actual behaviorThrows PlatformNotSupportedException. Regression?Yes, works in .NET Framework Known WorkaroundsUse reflection to set the _user field to null, make the call and set it back to the original value, like this: HttpListenerContext ctx = await listener.GetContextAsync(); IPrincipal originalPrincipal = ctx.User; // make the call with User = null to prevent the exception // revert to the original value Configuration.NET Core 3.1, Windows 10 x64 Other informationNo response
|
Can you give us simple self-contained repro we can run? (I assume the above code snippet is not sufficient) |
I will try to provide a repro, but it will take more time.
What we found so far was that the exception is actually thrown from HttpListenerWebSocketContext.CopyPrincipal, please see line 104 here:
runtime/HttpListenerWebSocketContext.cs at main · dotnet/runtime (github.com)<https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.HttpListener/src/System/Net/WebSockets/HttpListenerWebSocketContext.cs>
I don’t understand why WindowsPrincipal is not supported on Windows…
Yes, the same issue exists in .NET 6.
--
Can you give us simple self-contained repro we can run? (I assume the above code snippet is not sufficient)
Can you please test it also on .NET 6?
—
Reply to this email directly, view it on GitHub<#63738 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AENLQ3NA47QTMFSMAWUUL6TUV4ASLANCNFSM5L33DOBQ>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
|
Here is the repro:
using System;
using System.Diagnostics;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
namespace WebSocketCloseTest
{
public class Program
{
public static async Task Main(string[] args)
{
var serverTask = Server(http://localhost:45000/test/ws/);
await Client("ws://localhost:45000/test/ws/");
await serverTask;
}
private static async Task Server(String serverAddress)
{
var listener = new HttpListener();
listener.Prefixes.Add(serverAddress);
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
listener.Start();
HttpListenerContext ctx = await listener.GetContextAsync();
try
{
Debug.Assert(ctx.User != null); // this is supposed to be a WindowsPrincipal
HttpListenerWebSocketContext wsCtx = await ctx.AcceptWebSocketAsync("subProtocol"); // this throws on .NET Core
await wsCtx.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
}
finally
{
ctx.Response.Close();
}
}
private static async Task Client(String serverAddress)
{
try
{
using (ClientWebSocket webSocket = new ClientWebSocket())
{
webSocket.Options.AddSubProtocol("subProtocol");
webSocket.Options.Credentials = CredentialCache.DefaultCredentials;
await webSocket.ConnectAsync(new Uri(serverAddress), CancellationToken.None);
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
}
}
catch (Exception e)
{
Console.WriteLine($"Client WS exception: {e.Message}");
}
}
}
}
…--
Jan Burian, Senior Software Architect
t: +420 377 183 420 • iconics.com<https://iconics.com/>
ICONICS – A Group Company of Mitsubishi Electric
Register HERE<https://iconics.com/connect2021> for ICONICS Connect 2021!
***@***.***<https://iconics.com/newsletter> [A picture containing drawingDescription automatically generated] <https://www.linkedin.com/company/iconics> [A picture containing animal, birdDescription automatically generated] <https://www.twitter.com/ICONICS> [A picture containing kit, drawingDescription automatically generated] <https://www.facebook.com/ICONICSInc> [A picture containing drawingDescription automatically generated] <https://www.youtube.com/user/IconicsVideo>
From: Jan Burian
Sent: Friday, January 14, 2022 9:30 AM
To: dotnet/runtime ***@***.***>; dotnet/runtime ***@***.***>
Cc: Czech_Engr_Jan Burian ***@***.***>; Author ***@***.***>
Subject: RE: [dotnet/runtime] HttpListenerContext.AcceptWebSocketAsync unsupported for IWindowsPrincipal in .NET Core 3.1 (Issue #63738)
I will try to provide a repro, but it will take more time.
What we found so far was that the exception is actually thrown from HttpListenerWebSocketContext.CopyPrincipal, please see line 104 here:
runtime/HttpListenerWebSocketContext.cs at main · dotnet/runtime (github.com)<https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.HttpListener/src/System/Net/WebSockets/HttpListenerWebSocketContext.cs>
I don’t understand why WindowsPrincipal is not supported on Windows…
--
Jan Burian, Senior Software Architect
t: +420 377 183 420 • iconics.com<https://iconics.com/>
ICONICS – A Group Company of Mitsubishi Electric
Register HERE<https://iconics.com/connect2021> for ICONICS Connect 2021!
***@***.***<https://iconics.com/newsletter> [A picture containing drawingDescription automatically generated] <https://www.linkedin.com/company/iconics> [A picture containing animal, birdDescription automatically generated] <https://www.twitter.com/ICONICS> [A picture containing kit, drawingDescription automatically generated] <https://www.facebook.com/ICONICSInc> [A picture containing drawingDescription automatically generated] <https://www.youtube.com/user/IconicsVideo>
From: Karel Zikmund ***@***.******@***.***>>
Sent: Thursday, January 13, 2022 6:02 PM
To: dotnet/runtime ***@***.******@***.***>>
Cc: Czech_Engr_Jan Burian ***@***.******@***.***>>; Author ***@***.******@***.***>>
Subject: Re: [dotnet/runtime] HttpListenerContext.AcceptWebSocketAsync unsupported for IWindowsPrincipal in .NET Core 3.1 (Issue #63738)
Can you give us simple self-contained repro we can run? (I assume the above code snippet is not sufficient)
Can you please test it also on .NET 6?
—
Reply to this email directly, view it on GitHub<#63738 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AENLQ3NA47QTMFSMAWUUL6TUV4ASLANCNFSM5L33DOBQ>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
|
Indeed, as @iconics-janb pointed out, our code will throw PNSE if user is WindowsPrincipal: Line 104 in 57bfe47
but .NET Framework code contained an implementation for the same condition https://referencesource.microsoft.com/#System/net/System/Net/WebSockets/HttpListenerWebSocketContext.cs,150 @wfurt @stephentoub Do you know anything that would prevent us from reusing the original code with WindowsPrincipal existing in .NET Framework? I wonder why it was not used in the first place, and why PNSE was used (The PR that introduced it (dotnet/corefx#20398 (comment)) doesn't help). I would expect that user couldn't be WindowsPrincipal on non-Windows platform in the first place - am I missing something? |
Yes, I think we can make it work for Windows (assuming all the dependencies are in place) and PNSE for other platforms. |
Triage: Needs to be ported from .NET Framework. Fairly straightforward work - see link to referencesource above. |
Description
HttpListenerContext.AcceptWebSocketAsync throws PlatformNotSupportedException when called with User set to WindowsPrincipal. This happens in .NET Core 3.1 when running on Windows. Works fine when running in .NET Framework.
Reproduction Steps
Expected behavior
It should not throw the exception and work the same way as it does in .NET Framework
Actual behavior
Throws PlatformNotSupportedException.
Regression?
Yes, works in .NET Framework
Known Workarounds
Use reflection to set the _user field to null, make the call and set it back to the original value, like this:
Configuration
.NET Core 3.1, Windows 10 x64
Other information
No response
The text was updated successfully, but these errors were encountered: