如何向我的 IoT 中心连接字符串添加发送权限?

How do I add Send permission to my IoT Hub connection string?

我正在创建一个需要将消息发送到 Azure IoT 中心的 Azure 函数。当函数尝试发送时,出现以下错误:

2018-04-12T20:19:28.567 [Error] Microsoft.ServiceBus: Unauthorized access. 'Send' claim(s) are required to perform this operation. Resource: 'sb://iothub-ns-monitorhub-xxxxx-xxxxxxxx.servicebus.windows.net/monitorhub'. TrackingId:a79b2055d85446bd9739bd229aada0d7_G1, SystemTracker:gateway5 

看来我需要向集线器连接字符串添加发送声明。但是,当我转到中心的命名空间并单击共享访问策略时,我看到了:

权限列表不包含 'Send' 权限。如何为集线器的连接字符串添加发送权限?

以下是发生错误的Azure函数。代码编译没有错误,但会产生 运行 时间错误。

#r "Microsoft.ServiceBus"

using System;
using System.Text;
using Microsoft.ServiceBus.Messaging;

public static void Run(EventData eventData, out string outputEventHubMessage, TraceWriter log)
{
    // Get some system properties from the SystemProperties dictionary
    var deviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString();
    var messageSource = eventData.SystemProperties["iothub-message-source"].ToString();
    var enqueuedTime = eventData.SystemProperties["iothub-enqueuedtime"].ToString();
    var sequenceNumber = eventData.SystemProperties["SequenceNumber"].ToString();
    var offset = eventData.SystemProperties["Offset"].ToString();

    var data = Encoding.UTF8.GetString(eventData.GetBytes());
    var message = string.Format("Message Source: {0}; Enqueued Time: {1}; Sequence Number: {2}; Offset: {3}; DeviceId: {4}; Data: {5}", messageSource, enqueuedTime, sequenceNumber, offset, deviceId, data);

    outputEventHubMessage = message;

    log.Info($"{message}");
}

根据您更新后的代码和评论,了解到您想将事件发送到内置于事件中心兼容端点的 Azure 物联网中心。但是只能用来接收D2C消息

IoT Hub exposes the messages/events built-in endpoint for your back-end services to read the device-to-cloud messages received by your hub. This endpoint is Event Hub-compatible, which enables you to use any of the mechanisms the Event Hubs service supports for reading messages.

参考"Read device-to-cloud messages from the built-in endpoint".

Azure Function 通常由来自 azure 物联网中心的设备消息触发,并且可以将这些消息路由到 Azure 存储或采取其他操作。

如果您仍想从 azure 函数发送 D2C 消息或有任何其他疑虑,请随时告诉我。

部分参考资料:

Processing data from IoT Hub with Azure Functions

Send device-to-cloud messages to IoT Hub

Send simulated telemetry