无法连接托管在 Azure Web 应用程序上的安全 Asp.Net 核心 Web 套接字(使用 TLS)
Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS)
我花了一整天的时间寻找解决方案,但没有解决我的问题。从标题中可以看出,我使用 Asp.Net 核心 (3.1) 框架实现了一个基本的 Web 套接字,并将其部署在 Azure 上(在 Web 应用程序服务上)。 我成功地让它在没有 TLS 协议的情况下工作(所以我认为 ws 已经配置得很好)但是当我尝试使用 wss[=30= 连接时] 我在客户端收到此错误:
System.Net.WebSockets.WebSocketException : Unable to connect to the remote server
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
我试图在 Azure 门户上切换“仅 HTTPS”触发器,但它一直拒绝任何客户端连接。
你知道如何让 wss 与 Azure Web App 一起工作吗?我需要配置证书吗?我读到如果用户没有证书,天蓝色会提供证书。感谢和问候
更新:代码已从 this great Les Jackson's tutorial and is available on git hub at this precise address“复制”而来。代码的重要部分在这里:
/* THIS IS THE SERVER PART WHERE THE CONNECTION IS ACCEPTED */
namespace WebSocketServer.Middleware
{
public class WebSocketServerMiddleware
{
private readonly RequestDelegate _next;
private WebSocketServerConnectionManager _manager;
public WebSocketServerMiddleware(RequestDelegate next, WebSocketServerConnectionManager manager)
{
_next = next;
_manager = manager;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.WebSockets.IsWebSocketRequest)
{
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Receive(webSocket, async (result, buffer) =>
{
if (result.MessageType == WebSocketMessageType.Text)
{
Console.WriteLine($"Receive->Text");
return;
}
else if (result.MessageType == WebSocketMessageType.Close)
{
await sock.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
return;
}
});
}
else
{
await _next(context);
}
}
}
}
/* THIS IS THE STARTUP FILE*/
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddWebSocketServerConnectionManager();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseWebSockets();
app.UseWebSocketServer();
}
}
/* THIS IS THE CLIENT (WRITTEN IN NET FULLFRAMEWORK) */
Console.Write("Connecting....");
var cts = new CancellationTokenSource();
var socket = new ClientWebSocket();
string wsUri = "wss://testingwebsocket123123.azurewebsites.net";
await socket.ConnectAsync(new Uri(wsUri), cts.Token);
Console.WriteLine(socket.State);
await Task.Factory.StartNew(
async () =>
{
var rcvBytes = new byte[1024 * 1024];
var rcvBuffer = new ArraySegment<byte>(rcvBytes);
while (true)
{
WebSocketReceiveResult rcvResult = await socket.ReceiveAsync(rcvBuffer, cts.Token);
byte[] msgBytes = rcvBuffer.Skip(rcvBuffer.Offset).Take(rcvResult.Count).ToArray();
string rcvMsg = Encoding.UTF8.GetString(msgBytes);
Console.WriteLine("Received: {0}", rcvMsg);
}
}, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
感谢阅读
正如评论中所讨论的那样,它与 sample 中提供的 javascript 客户端一起工作正常。当您从具有完整框架的 c# 客户端连接时,.net 客户端中的错误是由于 TLS 版本而发生的。 Azure Web 应用程序的屏幕截图强制执行最低 TLS 1.2。在 .net 客户端中进行如下设置:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
我花了一整天的时间寻找解决方案,但没有解决我的问题。从标题中可以看出,我使用 Asp.Net 核心 (3.1) 框架实现了一个基本的 Web 套接字,并将其部署在 Azure 上(在 Web 应用程序服务上)。 我成功地让它在没有 TLS 协议的情况下工作(所以我认为 ws 已经配置得很好)但是当我尝试使用 wss[=30= 连接时] 我在客户端收到此错误:
System.Net.WebSockets.WebSocketException : Unable to connect to the remote server
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
我试图在 Azure 门户上切换“仅 HTTPS”触发器,但它一直拒绝任何客户端连接。
你知道如何让 wss 与 Azure Web App 一起工作吗?我需要配置证书吗?我读到如果用户没有证书,天蓝色会提供证书。感谢和问候
更新:代码已从 this great Les Jackson's tutorial and is available on git hub at this precise address“复制”而来。代码的重要部分在这里:
/* THIS IS THE SERVER PART WHERE THE CONNECTION IS ACCEPTED */
namespace WebSocketServer.Middleware
{
public class WebSocketServerMiddleware
{
private readonly RequestDelegate _next;
private WebSocketServerConnectionManager _manager;
public WebSocketServerMiddleware(RequestDelegate next, WebSocketServerConnectionManager manager)
{
_next = next;
_manager = manager;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.WebSockets.IsWebSocketRequest)
{
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Receive(webSocket, async (result, buffer) =>
{
if (result.MessageType == WebSocketMessageType.Text)
{
Console.WriteLine($"Receive->Text");
return;
}
else if (result.MessageType == WebSocketMessageType.Close)
{
await sock.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
return;
}
});
}
else
{
await _next(context);
}
}
}
}
/* THIS IS THE STARTUP FILE*/
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddWebSocketServerConnectionManager();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseWebSockets();
app.UseWebSocketServer();
}
}
/* THIS IS THE CLIENT (WRITTEN IN NET FULLFRAMEWORK) */
Console.Write("Connecting....");
var cts = new CancellationTokenSource();
var socket = new ClientWebSocket();
string wsUri = "wss://testingwebsocket123123.azurewebsites.net";
await socket.ConnectAsync(new Uri(wsUri), cts.Token);
Console.WriteLine(socket.State);
await Task.Factory.StartNew(
async () =>
{
var rcvBytes = new byte[1024 * 1024];
var rcvBuffer = new ArraySegment<byte>(rcvBytes);
while (true)
{
WebSocketReceiveResult rcvResult = await socket.ReceiveAsync(rcvBuffer, cts.Token);
byte[] msgBytes = rcvBuffer.Skip(rcvBuffer.Offset).Take(rcvResult.Count).ToArray();
string rcvMsg = Encoding.UTF8.GetString(msgBytes);
Console.WriteLine("Received: {0}", rcvMsg);
}
}, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
感谢阅读
正如评论中所讨论的那样,它与 sample 中提供的 javascript 客户端一起工作正常。当您从具有完整框架的 c# 客户端连接时,.net 客户端中的错误是由于 TLS 版本而发生的。 Azure Web 应用程序的屏幕截图强制执行最低 TLS 1.2。在 .net 客户端中进行如下设置:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;