簡體   English   中英

無法連接托管在 Azure Web 應用程序上的安全 Asp.Net Core Web 套接字(使用 TLS)

[英]Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS)

我花了一整天的時間尋找解決方案,但沒有解決我的問題。 從標題中可以看出,我使用 Asp.Net Core (3.1) 框架實現了一個基本的 Web Socket,並將其部署在 Azure 上(在 ZC6E190B284633C48E39E55049 AppDA3 服務上) 我成功地使它在沒有 TLS 協議的情況下工作(所以我認為 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”觸發器,但它一直拒絕任何客戶端連接。 在此處輸入圖像描述

您知道如何讓 wss 與 Azure Web App 一起使用嗎? 我需要配置證書嗎? 我讀到如果用戶沒有證書,azure 會提供證書。 謝謝並恭祝安康


更新:代碼已從這個偉大的 Les Jackson 教程中“復制”,可在 git 集線器上的這個精確地址上找到。 代碼的重要部分在這里:

/* 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);

感謝您閱讀

正如評論所討論的那樣,它可以與示例中提供的 javascript 客戶端一起正常工作。 當您從具有完整框架的 c# 客戶端連接時,.net 客戶端中的錯誤是由於 TLS 版本而發生的。 您的 Azure web 應用程序的屏幕截圖強制執行最低 TLS 1.2。 在 .net 客戶端中設置如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM