Skip to content

Commit d46f1ea

Browse files
committed
Add DefautCredentials_WebSocket_Success
1 parent d8038b0 commit d46f1ea

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/Security/Authentication/Negotiate/test/Negotiate.FunctionalTest/Microsoft.AspNetCore.Authentication.Negotiate.FunctionalTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Reference Include="Microsoft.AspNetCore.Authentication.Negotiate" />
1010
<Reference Include="Microsoft.AspNetCore.Routing" />
1111
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
12+
<Reference Include="Microsoft.AspNetCore.WebSockets" />
1213
<Reference Include="Microsoft.Extensions.Hosting" />
1314
<Reference Include="System.Net.Http.WinHttpHandler" />
1415
</ItemGroup>

src/Security/Authentication/Negotiate/test/Negotiate.FunctionalTest/NegotiateHandlerFunctionalTests.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using System.Linq;
77
using System.Net;
88
using System.Net.Http;
9+
using System.Net.WebSockets;
10+
using System.Text;
11+
using System.Threading;
912
using System.Threading.Tasks;
1013
using Microsoft.AspNetCore.Builder;
1114
using Microsoft.AspNetCore.Hosting;
@@ -23,7 +26,7 @@ namespace Microsoft.AspNetCore.Authentication.Negotiate
2326
{
2427
// In theory this would work on Linux and Mac, but the client would require explicit credentials.
2528
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
26-
public class NegotiateHandlerFunctionalTests
29+
public class NegotiateHandlerFunctionalTests : LoggedTest
2730
{
2831
private static readonly Version Http11Version = new Version(1, 1);
2932
private static readonly Version Http2Version = new Version(2, 0);
@@ -109,6 +112,34 @@ public async Task DefautCredentials_Success(Version version)
109112
Assert.Equal(Http11Version, result.Version); // HTTP/2 downgrades.
110113
}
111114

115+
[ConditionalFact]
116+
public async Task DefautCredentials_WebSocket_Success()
117+
{
118+
using var host = await CreateHostAsync();
119+
120+
var address = host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses.First().Replace("https://", "wss://");
121+
122+
using var webSocket = new ClientWebSocket
123+
{
124+
Options =
125+
{
126+
RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true,
127+
UseDefaultCredentials = true,
128+
}
129+
};
130+
131+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
132+
133+
await webSocket.ConnectAsync(new Uri($"{address}/AuthenticateWebSocket"), cts.Token);
134+
135+
var receiveBuffer = new byte[13];
136+
var receiveResult = await webSocket.ReceiveAsync(receiveBuffer, cts.Token);
137+
138+
Assert.True(receiveResult.EndOfMessage);
139+
Assert.Equal(WebSocketMessageType.Text, receiveResult.MessageType);
140+
Assert.Equal("Hello World!", Encoding.UTF8.GetString(receiveBuffer, 0, receiveResult.Count));
141+
}
142+
112143
public static IEnumerable<object[]> HttpOrders =>
113144
new List<object[]>
114145
{
@@ -232,9 +263,10 @@ public async Task UnauthorizedAfterAuthenticated_Success(Version version)
232263
Assert.Equal(Http11Version, result.Version); // HTTP/2 downgrades.
233264
}
234265

235-
private static Task<IHost> CreateHostAsync(Action<NegotiateOptions> configureOptions = null)
266+
private Task<IHost> CreateHostAsync(Action<NegotiateOptions> configureOptions = null)
236267
{
237268
var builder = new HostBuilder()
269+
.ConfigureServices(AddTestLogging)
238270
.ConfigureServices(services => services
239271
.AddRouting()
240272
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
@@ -252,6 +284,7 @@ private static Task<IHost> CreateHostAsync(Action<NegotiateOptions> configureOpt
252284
{
253285
app.UseRouting();
254286
app.UseAuthentication();
287+
app.UseWebSockets();
255288
app.UseEndpoints(ConfigureEndpoints);
256289
});
257290
});
@@ -289,6 +322,27 @@ private static void ConfigureEndpoints(IEndpointRouteBuilder builder)
289322
await context.Response.WriteAsync(name);
290323
});
291324

325+
builder.Map("/AuthenticateWebSocket", async context =>
326+
{
327+
if (!context.User.Identity.IsAuthenticated)
328+
{
329+
await context.ChallengeAsync();
330+
return;
331+
}
332+
333+
if (!context.WebSockets.IsWebSocketRequest)
334+
{
335+
context.Response.StatusCode = 400;
336+
return;
337+
}
338+
339+
Assert.False(string.IsNullOrEmpty(context.User.Identity.Name), "name");
340+
341+
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
342+
343+
await webSocket.SendAsync(Encoding.UTF8.GetBytes("Hello World!"), WebSocketMessageType.Text, endOfMessage: true, context.RequestAborted);
344+
});
345+
292346
builder.Map("/AlreadyAuthenticated", async context =>
293347
{
294348
Assert.Equal("HTTP/1.1", context.Request.Protocol); // Not HTTP/2

src/Servers/Kestrel/test/InMemory.FunctionalTests/UpgradeTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
1111
using Microsoft.AspNetCore.Server.Kestrel.Tests;
1212
using Microsoft.AspNetCore.Testing;
13-
using Microsoft.Extensions.Logging.Testing;
1413
using Xunit;
1514

1615
namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests

0 commit comments

Comments
 (0)