Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Establishing Secure WebSocket Connection to AWS IoT MQTT Server on .NET Framework 4.x #1922

Open
jwielebnowski opened this issue Feb 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jwielebnowski
Copy link

I am encountering an issue when attempting to establish a secure WebSocket connection to the AWS IoT MQTT server using the MQTTnet library within a simple console application developed in Visual Studio 2022. Specifically, the connection attempt fails under the following conditions:

Environment: The application is compiled and run targeting .NET Framework 4.x.
Symptoms: The program fails to connect to the WebSocket server. However, when the application is compiled and run on .NET 6.0, everything functions as expected, with the exception of using MQTTnet.dll targeting netstandard2.0. In this scenario, the application again fails to connect to the AWS IoT MQTT WebSocket server.
Observation: It appears that any combination of .NET version and MQTTnet version operates correctly with unsecured connections. For instance, connecting to wss://test.mosquitto.org:8081 is consistently successful.

To Reproduce

Steps to reproduce the behavior:

  1. Create console application in Visual Studio
  2. Add routine to connect to the webSocket (see below the code)
  3. With these arguments '....'. (see code below). the preSigned url expired but if all works you will get 403 error. Otherwise you will get "Cannot connect to server"

Expected behavior

A clear and concise description of what you expected to happen.

Code example

Please provide full code examples below where possible to make it easier for the developers to check your issues.

        private async Task ConnectMqttAsync(string preSignedUrl)
        {
            // Create a new MQTT client.
            var factory = new MqttFactory();
            _mqttClient = factory.CreateMqttClient();

            // Subscribe to MQTTnet client events
            _mqttClient.ConnectedAsync += async e =>
            {
                Connected?.Invoke(this, EventArgs.Empty);
                await Task.CompletedTask;
            };

            _mqttClient.DisconnectedAsync += async e =>
            {
                Disconnected?.Invoke(this, EventArgs.Empty);
                await Task.CompletedTask;
            };

            _mqttClient.ApplicationMessageReceivedAsync += async e =>
            {
                MessageReceived?.Invoke(this, e);
                await Task.CompletedTask;
            };
         //   Console.WriteLine(preSignedUrl);
            preSignedUrl = @"wss://a1cam1ghby1n8b-ats.iot.us-west-2.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAROMFQA6XNDJ4HNX2%2F20240206%2Fus-west-2%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20240206T220237Z&X-Amz-SignedHeaders=host&X-Amz-Signature=8dadb8e129d93a5f4c301a2700ed5167d1fae8cfe0361d5685bfafac0fb055c4&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEOj%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJIMEYCIQCbdo5UUz%2BUrfCsOdE1Goy6rkWy5wYa0rW8Qv%2FHZTzSlgIhAJV%2BZWUySJoveT3CxoQfsz7mZpToC3LcNgzouzx2ey0zKp8FCLD%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEQBBoMMDk5NjAxMDkwNDc4IgxDkXxrKi%2FAtsE%2BM7Qq8wQMq19hriIArXtK1Ybca%2BCNzXGPEt4d4k5Q82Qe5gEDo4X%2BjXaEvha8%2FiNcVHwYHvCAr3RElSUP3Y0%2FpX0PkJsiE2hDHjcJWWQ5%2Bcqf8ltMzjSTr2qYh%2F9Sc668%2Be4I%2FLQXbdCzNVRhYTBpJXK2QsoNXDNvb9sivtF7WGaUgUm4TUBMtFFym62%2FJu%2BHjbCYtEYwwVhSja4Et5LHku%2BY1GZXqdQ2iclbAOROZOdzbGWxOj9dJgpPeVyKePzCNNYQKgUjzec6Q57l4bdGGxb8oDfMpx1ROBtD%2BOljmQIPgiWEIRaaHsNLY2rARzv57h7IFkyyyp87hWSitDiM1%2Bmc9Nh4itkvN73oZ0JW3n1sikVtc8qcL4r6%2F8tOqX0vrXWXhqkVLKgtEsRoIGg4xEKreO3PAuxFJSmA7l1Pt98GiOZvvZDQJ3rg7Srr1jEsqk%2Fx3%2ByEQqOhWBZQgs7QPSkFc0GX1LROJq3BDienEyjIgZSf02P2oh3S4zwUdRVyBazgT%2BE01B8JTxBHWNy7g1ZAHleyhiZz4K%2BRVVH3JedK1LfA%2BVwWf%2BfKrVBIw88quOlaaCsIC7ARwLdhun2XTWTniCL6xZWQsN8MMG3BkXVcws8r8atFVpMT4usiDmcJjn020UMavrJ3%2BOgc3xjKY1KqK4eB2MCf1oy8qxMj9wDUNJPQIC7OJ3%2FkSlucCQS4HQIw7fjQRtjE9lub%2Bb8zoCUwHmSFNxEBSio%2FabEs%2FMaveKPc4JOLdjmnHjL1L841SJPrXYHwgrKyAGRVz%2BCWRfwof1XnoEpCuGUE%2FefAd6J1WNzjpgcunqFjGFsTSTdY9jzzr3gWFdgww9qFrgY6nAE0UwgDB6n5zYolBQK8Kq0YwjS6J2gyFkIL6BzJ0mjgs08F64LEat5a66n%2FLQRrDDbE%2FfEnXYeVBjSHcP8%2FC5wvcIZT%2FR40yIA8Yu5VjNfxnC4FztdDqxYgsNmgW9%2BiU0zrDN5NGE40%2BIJsU679MJkrhlNJswy%2FwTK2q4ACOSLZgbc5pULLQi%2F14n8FUziG1XDIONoAtyuelawXBtY%3D";
            // Setup and configure the client options for WebSocket connection.
            var options = new MqttClientOptionsBuilder()
                .WithWebSocketServer(o => o.WithUri(preSignedUrl)) 
                .WithTlsOptions(o => { }) 
                .Build();


            try
            {
                Console.WriteLine(options.ChannelOptions.ToString());
                // Connect to the MQTT broker using the configured options.
                var response = await _mqttClient.ConnectAsync(options);
                Console.WriteLine("Connected successfully to MQTT broker.");

            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
 
            }


        }

This issue seems indicative of a issue within the library when handling secure connections, specifically under the .NET Framework 4.x environment. My application requires establishing a secure WebSocket connection with a signed URL in a Unity application, which supports .NET 4.x, and cannot be upgraded to a higher .NET version. Additionally, the application needs to be cross-platform and compatible with the IL2CPP compiler.

I am seeking suggestions on how to resolve or work around this problem to ensure successful secure connections under the specified conditions. Any assistance or insights from the community or maintainers would be greatly appreciated.

@jwielebnowski jwielebnowski added the bug Something isn't working label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant