为事件中心客户端应用程序配置传输层安全性 (TLS)

为了安全起见,Azure 事件中心命名空间可能要求客户端使用最低版本的传输层安全性 (TLS) 来发送请求。 如果客户端使用的 TLS 版本低于所需的最低版本,则对 Azure 事件中心的调用将失败。 例如,如果命名空间需要 TLS 1.2,则使用 TLS 1.1 的客户端发送的请求将失败。

本文介绍如何将客户端应用程序配置为使用特定版本的 TLS。

配置客户端 TLS 版本

为了使客户端能够使用特定版本的 TLS 发送请求,操作系统必须支持该版本。

以下示例演示如何通过 .NET 将客户端的 TLS 版本设置为 1.2。 客户端使用的 .NET Framework 必须支持 TLS 1.2。 有关详细信息,请参阅 TLS 1.2 支持

以下示例演示如何使用事件中心的 Azure.Messaging.ServiceBus 客户端库在 .NET 客户端中启用 TLS 1.2:

{
    // Enable TLS 1.2 before connecting to Event Hubs
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

    // Connection string to your Event Hubs namespace
    string connectionString = "<NAMESPACE CONNECTION STRING>";
    
    // Name of your Event Hub
    string eventHubName = "<EVENT HUB NAME>";
    
    // The sender used to publish messages to the queue
    var producer = new EventHubProducerClient(connectionString, eventHubName);
    
    // Use the producer client to send a message to the Event Hubs queue
    using EventDataBatch eventBatch = await producer.CreateBatchAsync();
    var eventData = new EventData("This is an event body");

    if (!eventBatch.TryAdd(eventData))
    {
        throw new Exception($"The event could not be added.");
    }
}

验证客户端使用的 TLS 版本

若要验证客户端是否使用指定版本的 TLS 发送请求,可以使用 Fiddler 或类似工具。 打开 Fiddler 以开始捕获客户端网络流量,然后执行上一节中的示例之一。 查看 Fiddler 跟踪,确认已使用正确版本的 TLS 发送请求。

后续步骤

有关详细信息,请参阅以下文档。