How to enable CORS for IOT Hub service

Heinrich Braasch 0 Reputation points
2024-12-18T01:29:27.1433333+00:00

I have trouble connecting to the IOT Hub from a webassembly app. It reports in the exception: Operation is not supported on this platform

I think it's a CORS problem, since I got the same exception accessing a different website, which resolved when enabling CORS.

Obviously, the exact code e.g. running in a desktop app, connects fine. So the code works.

Thanks. Help shall be much appreciated.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,222 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 33,956 Reputation points MVP
    2024-12-19T12:21:47.4133333+00:00

    Hello @Heinrich Braasch ,

    welcome to this moderated Azure community forum.

    I have never used the Azure IoT SDK within a WebAssembly environment.

    So, I'm not sure how to fix this cross origin challenge within Webassembly.

    It seems the app is used for controlling devices via the IoT Hub (hence the direct method invocation).

    I propose an alternative.

    Do you have a Azure server-side backend?

    If so, a solution could be to make a secure call from the webassembly layer to the backend.

    In the backend, you invoke the actual call to the IoT Hub via the IoT Hub connection string.

    This is also a much more secure solution!

    It seems your webassembly has the IoT Hub connection string which is not advised, storing this key outside the cloud.

    Is this a solution you could work with?


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.