2017-02-28 49 views
1

在AMQP lite github頁面上的示例之後,我已成功使用SASL ANONYMOUS然後在$ cbs端點上使用令牌連接。 (https://github.com/Azure/amqpnetlite/blob/master/docs/articles/service_to_iothub.md使用AMQP lite使用共享訪問策略和密鑰連接到IoT Hub

在每個請求上生成SAS令牌都是多餘的,我想在連接到IoT Hub時使用共享訪問策略和密鑰作爲用戶和密碼。 我遵循https://github.com/Azure/amqpnetlite/blob/master/docs/articles/building_application.md#specifying-an-address的文檔以及https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-security#protocol-specifics上的說明,但是我一直使用代碼「Sys」得到一些未經驗證的錯誤。

憑證當然是好的,因爲我使用它們來生成SAS令牌,並且我嘗試了用戶名(帶/不帶域)的變體,但沒有成功。我特別檢查了用戶名和密碼是由URL編碼的。

遵循SASL標準的文檔,錯誤代碼Sys似乎指示接收端的一些問題(http://www.rfc-base.org/txt/rfc-3206.txt第3頁)。

有其他人遇到這個問題嗎?有沒有解決方案,或者有可能以這種方式連接到物聯網集線器?

感謝, 喬治

+0

>在每次請求生成SAS令牌是多餘的。只需指出,您不必爲每個請求生成新的SAS令牌。 – bearrito

回答

1

我得到「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(); 
    } 

欲瞭解更多信息,你可以參考「CONNECTING TO THE AZURE IOT HUB USING AN AMQP STACK」。

更新:

代替使用SAS令牌像上面的代碼塊,下面的代碼使用共享訪問策略和密鑰(SASL PLAIN)的:

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 = "[email protected][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(); 
    } 
+0

我設法使用SAS令牌連接到IoT,但我想在沒有SAS令牌的情況下執行此操作。當我爲連接創建地址時,我想使用共享訪問策略和密鑰作爲用戶密碼,在這種情況下,我會使用SASL PLAIN。 –

+0

@GeorgeOnofrei我添加使用SASL PLAIN的代碼部分。你可以檢查答案。這個對我有用。如果有任何問題,請隨時通知我。 –

+0

謝謝!代碼看起來沒問題,並且與github上的某些指示方向相同。以下是對該問題的參考:https://github.com/Azure/amqpnetlite/issues/195 –

相關問題