Skip to content

Commit 5e71ad9

Browse files
committed
Add DefautCredentials_WebSocket_Success
1 parent 2e2f2c5 commit 5e71ad9

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
@@ -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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
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;
1215
using Microsoft.AspNetCore.Hosting.Server;
1316
using Microsoft.AspNetCore.Hosting.Server.Features;
1417
using Microsoft.AspNetCore.Http;
18+
using Microsoft.AspNetCore.Http.Features;
1519
using Microsoft.AspNetCore.Routing;
1620
using Microsoft.AspNetCore.Testing;
1721
using Microsoft.Extensions.DependencyInjection;
@@ -109,6 +113,34 @@ public async Task DefautCredentials_Success(Version version)
109113
Assert.Equal(Http11Version, result.Version); // HTTP/2 downgrades.
110114
}
111115

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

0 commit comments

Comments
 (0)