gpt4 book ai didi

c# - 无法连接 Azure Web App 上托管的安全 Asp.Net Core Web 套接字(使用 TLS)

转载 作者:行者123 更新时间:2023-12-03 01:26:14 24 4
gpt4 key购买 nike

我花了一整天的时间寻找解决方案,但没有解决我的问题。从标题中可以看出,我使用 Asp.Net Core (3.1) 框架实现了一个基本的 Web Socket,并将其部署在 Azure 上(在 Web 应用服务上)。 我成功地使其在没有 TLS 协议(protocol)的情况下工作(因此我认为 ws 已以良好的方式配置),但是当我尝试使用 wss 连接时,我收到此错误客户端:

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”触发器,但它始终拒绝任何客户端连接。 enter image description here

您知道如何让 wss 与 Azure Web App 配合使用吗?我需要配置证书吗?我读到,如果用户没有证书, azure 会提供证书。感谢和问候

<小时/>

更新:代码已从this great Les Jackson's tutorial“复制”可以在 git hub 上找到 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# 客户端连接时,由于 TLS 版本,.net 客户端中会发生错误。 Azure Web 应用程序的屏幕截图强制执行最低 TLS 1.2。在 .net 客户端中进行如下设置:

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; 

关于c# - 无法连接 Azure Web App 上托管的安全 Asp.Net Core Web 套接字(使用 TLS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65898388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com