How to resolve Kepware Azure IoT hub MQTT agent communication failures?

Tim Parsons 0 Reputation points
2025-11-20T16:30:19.59+00:00

Support Request Summary — Azure IoT Hub MQTT Authentication Failure with Kepware We are attempting to connect Kepware KEPServerEX IoT Gateway (MQTT Agent) to our Azure IoT Hub device kepware_acc_staug_002, but Azure consistently returns:

MQTT agent MqttSecurityException 'Agent' failed to connect - reason: 'Not authorized to connect'

This happens immediately after a successful TLS connection, which confirms:

DNS resolution works

Port 8883 is open

TLS handshake succeeds

The MQTT CONNECT packet is accepted

Network or firewall issues are not the cause

We have validated that the IoT Hub MQTT endpoint is reachable:

MQTT agent 'Minibuck' is connected to broker 'ssl://IOT-ACC-StAug-DB.azure-devices.net:8883'

But IoT Hub immediately rejects authentication.


What we have already tried (condensed technical checklist) 1. SAS Token Validity Generated SAS tokens using:

Our own PowerShell script

  Azure Cloud Shell (`az iot hub generate-sas-token`) — multiple attempts  
  Verified SAS token structure: `sr=`, `sig=`, `se=` all present
  Verified tokens are single-line, no truncation, no invalid characters
  Confirmed the SAS token resource URI matches the HostName and DeviceId exactly

2. Kepware Credential Alignment Updated Kepware MQTT Username to match the exact sr= host casing from the SAS token

Verified MQTT Username format as per Azure documentation: {hostname}/{deviceId}/?api-version=2018-06-30

Ensured:

Client ID = deviceId Username = exact host/deviceId/api-version Password = full SAS token 3. Device Configuration in Azure

Device exists: kepware_acc_staug_002

Status is Enabled

Primary key is present

Using the Primary Device Connection String, not policy keys

Confirmed no duplicate device IDs and no trailing whitespace

4. Hub & Device Properties

Verified no regeneration of device keys during testing

Verified SAS tokens were signed with the correct SharedAccessKey

Hub name: IOT-ACC-StAug-DB

Device path: /devices/kepware_acc_staug_002

5. Kepware Operational Checks

Kepware IoT Gateway service runs correctly

TLS v1.2 is enabled

No certificate errors

Kepware consistently reports successful connection to the broker before the auth failure

Symptoms

Every connection attempt results in the same sequence:

MQTT agent 'Minibuck' is connected to broker 'ssl://IOT-ACC-StAug-DB.azure-devices.net:8883'
MQTT agent MqttSecurityException 'Agent' failed to connect - reason: 'Not authorized to connect'
MQTT agent MqttException failed to InterruptedException

What we need from Microsoft

We need assistance determining why Azure IoT Hub is rejecting MQTT authentication despite:

Valid SAS tokens generated via Azure CLI

Correctly formatted username/clientId

Matching casing between Hub Hostname, SAS token, and Username

Enabled device with correct primary key

Successful TLS connect

This looks like an authentication-layer rejection at IoT Hub and we need help reviewing the relevant logs or device-level configuration issues that could cause persistent "Not authorized to connect" responses even with verified credentials.


If you’d like, I can also prepare a second attachment for Microsoft showing:

  • The exact sr= and se= fields of the SAS token

The Kepware username string

The relevant Kepware log excerpt

A diagram of the Kepware → IoT Hub flow

Just say the word.Here is a clean, concise, professional explanation you can paste directly into a Microsoft support ticket.

This version explains the issue clearly without overwhelming detail, while proving you’ve ruled out all the usual causes.

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

1 answer

Sort by: Most helpful
  1. Nikhil Jha (Accenture International Limited) 4,150 Reputation points Microsoft External Staff Moderator
    2025-12-02T07:16:57.5733333+00:00

    Hello Tim Parsons,
    I understand you are encountering a persistent MqttSecurityException ("Not authorized to connect") when connecting Kepware to Azure IoT Hub via MQTT, despite a successful TLS handshake and network validation.

    Based on the detailed context you provided, the network and TLS layers are functioning correctly. The error occurring immediately after the TLS handshake confirms that the IoT Hub is rejecting the specific MQTT CONNECT packet credentials.

    This is almost exclusively caused by a mismatch in the strict formatting requirements Azure IoT Hub enforces for the MQTT protocol fields (Username, ClientID, and Password). Even a minor deviation here causes an immediate disconnect.

    **
    I would suggest you to please verify the following three configurations in your Kepware IoT Gateway agent.

    1.Validate the MQTT "User Name" Field

    This is the most common cause of 401/Not Authorized errors in direct MQTT connections. Azure IoT Hub requires a specific string format that includes the API version. If the api-version is missing, auth fails.

    • Format: {IoT Hub Hostname}/{Device ID}/?api-version=2021-04-12
    • Your Configuration should look exactly like this: IOT-ACC-StAug-DB.azure-devices.net/kepware_acc_staug_002/?api-version=2021-04-12
      • Check: Ensure the Hub Hostname is the full FQDN.
      • Check: Ensure the /?api-version=2021-04-12 is appended at the end.

    2.Validate the MQTT "Client ID" Field

    In the MQTT protocol, Azure IoT Hub strictly requires the MQTT Client ID to be identical to the Device ID registered in the hub.

    • Kepware Setting: In the IoT Gateway agent settings, look for the Client ID field.
    • Value: kepware_acc_staug_002
    • Note: If Kepware is generating a random Client ID or appending a string to it, the connection will be rejected.

    3.Validate the SAS Token (Password) Structure

    You mentioned using Azure CLI to generate the token, which is good. However, confirm the "Resource URI" (sr) embedded within that token matches the casing of your inputs exactly.

    • The token should look like: SharedAccessSignature sr=IOT-ACC-StAug-DB.azure-devices.net%2Fdevices%2Fkepware_acc_staug_002&sig=...&se=...
    • Double Check: Paste the generated token into the Password field in Kepware. Ensure no trailing spaces were copied.

    4.Isolation Test (Recommended)

    To rule out Kepware-specific formatting issues, try connecting with a standalone tool like MQTT Explorer or MQTT.fx using the exact same credentials:

    • Host: IOT-ACC-StAug-DB.azure-devices.net
    • Port: 8883
    • Username: (As defined in Step 1)
    • Password: (Your SAS Token)
    • Client ID: kepware_acc_staug_002
    • TLS: On (CA Certificate required)

    If this tool connects successfully, the credentials are valid, and the issue lies in how Kepware is constructing the packet (e.g., hidden characters or specific TLS version settings). If this tool also fails, the issue is definitely with the SAS token generation or Device ID casing.

    Reference:

    1. Communicate with your IoT hub using the MQTT protocol
    2. Control access to IoT Hub using SAS
    0 comments No comments

Your answer

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