Azure 使用共享访问策略和密钥连接到使用AMQP lite的物联网中心

Azure 使用共享访问策略和密钥连接到使用AMQP lite的物联网中心,azure,authentication,amqp,sasl,azure-iot-hub,Azure,Authentication,Amqp,Sasl,Azure Iot Hub,按照AMQP lite github页面上的示例,我已经成功地使用$cbs端点上的SASL ANONYMOUS then令牌进行了连接。() 在每个请求上生成SAS令牌是多余的,我希望在连接到IoT Hub时使用共享访问策略和密钥作为用户和密码。 我已经按照上的文档以及上的指示进行了操作,但我不断收到一些未经验证的错误,代码为“Sys” 凭据当然很好,因为我使用它们来生成SAS令牌,并且我尝试了用户名的各种变化(有/没有域),但没有成功。我特别检查了用户名和密码是否为URL编码 根据SASL标准

按照AMQP lite github页面上的示例,我已经成功地使用$cbs端点上的SASL ANONYMOUS then令牌进行了连接。()

在每个请求上生成SAS令牌是多余的,我希望在连接到IoT Hub时使用共享访问策略和密钥作为用户和密码。 我已经按照上的文档以及上的指示进行了操作,但我不断收到一些未经验证的错误,代码为“Sys”

凭据当然很好,因为我使用它们来生成SAS令牌,并且我尝试了用户名的各种变化(有/没有域),但没有成功。我特别检查了用户名和密码是否为URL编码

根据SASL标准的文档,错误代码Sys似乎表明接收方存在一些问题(第3页)

还有其他人遇到过这个问题吗?是否有任何解决方案或可以通过这种方式连接到物联网中心

谢谢, George

由于未发送put令牌消息,我收到“amqp:未经授权访问”错误:

        string sasToken = GetSharedAccessSignature(null, device_key, resourceUri, new TimeSpan(1, 0, 0));
        bool cbs = PutCbsToken(connection, host, sasToken, audience);
我使用以下代码在Windows 10桌面上进行测试,它可用于发送和接收:

    static string host = "[IOT_HUB_NAME].azure-devices.net";
    static int port = 5671;
    static string device_id = "[DEVICE_ID]";
    static string device_key = "[DEVICE_KEY]";
    static Session session;

    static void Main(string[] args)
    {
        Address address = new Address(host, port, null, null);
        Connection connection = new Connection(address);

        string audience = Fx.Format("{0}/devices/{1}", host, device_id);
        string resourceUri = Fx.Format("{0}/devices/{1}", host, device_id);

        string sasToken = GetSharedAccessSignature(null, device_key, resourceUri, new TimeSpan(1, 0, 0));
        bool cbs = PutCbsToken(connection, host, sasToken, audience);

        session = new Session(connection);

        SendEvent();
        ReceiveCommands();
        Console.ReadLine();
    }

    static private void SendEvent()
    {
        string entity = Fx.Format("/devices/{0}/messages/events", device_id);

        SenderLink senderLink = new SenderLink(session, "sender-link", entity);

        var messageValue = Encoding.UTF8.GetBytes("i am a message.");
        Message message = new Message()
        {
            BodySection = new Data() { Binary = messageValue }
        };

        senderLink.Send(message);
        senderLink.Close();
    }

    static private void ReceiveCommands()
    {
        string entity = Fx.Format("/devices/{0}/messages/deviceBound", device_id);

        ReceiverLink receiveLink = new ReceiverLink(session, "receive-link", entity);

        Message received = receiveLink.Receive();
        if (received != null)
            receiveLink.Accept(received);
        Console.WriteLine(received.BodySection.ToString());
        receiveLink.Close();
    }
有关详细信息,请参考“”

更新:

下面的代码使用共享访问策略和密钥(SASL平原),而不是像上面的代码段那样使用SAS令牌:


我设法使用SAS令牌连接到IoT,但我想在没有SAS令牌的情况下进行连接。创建连接地址时,我想使用共享访问策略和密钥作为用户密码,在这种情况下,我将使用SASL平原。@GeorgeOnofrei添加使用SASL平原的代码部分。你可以核对答案。它对我有用。如果有任何问题,请随时告诉我。谢谢!代码看起来不错,方向与github上的某些指示相同。这里有一个问题的参考:>在每个请求上生成SAS令牌是多余的。需要指出的是,您不必在每个请求上生成新的SAS令牌。
    static string host = "[IOT_HUB_NAME].azure-devices.net";
    static int port = 5671;
    static string device_id = "[DEVICE_ID]";
    static string device_key = "[DEVICE_KEY]";
    private const string username_hublevel = "iothubowner@sas.root.[IOT_HUB_NAME]";
    private const string password_hublevel = "SharedAccessSignature sr={URL-encoded-resourceURI}&sig={signature-string}&se={expiry}&skn={policyName}";

    static Session session;

    static void Main(string[] args)
    {
        Address address = new Address(host, port, username_hublevel, password_hublevel);
        Connection connection = new Connection(address);

        string audience = Fx.Format("{0}/devices/{1}", host, device_id);
        string resourceUri = Fx.Format("{0}/devices/{1}", host, device_id);

        session = new Session(connection);

        SendEvent();
        Console.WriteLine("Sent Hello AMQP!");
        ReceiveCommands();
        Console.ReadLine();
    }