【Azure 服务总线】使用Azure Service Bus 时,出现证书错误: 所使用的证书具有无法验证的信任链

简介: 【Azure 服务总线】使用Azure Service Bus 时,出现证书错误: 所使用的证书具有无法验证的信任链

问题描述

在Azure中连接 Service Bus 服务发送消息时发生证书错误,抛出证书异常消息:

The X.509 certificate CN=servicebus.chinacloudapi.cn, OU=Azure, O=Shanghai Blue Cloud Technology Co Ltd, L=Shanghai, S=Shanghai, C=CN is not in the trusted people store. 
The X.509 certificate CN=servicebus.chinacloudapi.cn, OU=Azure, O=Shanghai Blue Cloud Technology Co Ltd, L=Shanghai, S=Shanghai, C=CN chain building failed. 
The certificate that was used has a trust chain that cannot be verified. 
Replace the certificate or change the certificateValidationMode. 
A certificate chain could not be built to a trusted root authority.

 

 

问题解答

如果在连接Service Bus的代码中不对 ConnectivityMode 做预先设置,Service Bus SDK默认使用了 AutoDetect 模式 连接 Service Bus 服务。

AutoDetect 会优先使用 TCP 连接模式,由于 TCP 连接模式也是加密的,所以客户端需要首先验证 service bus 服务器证书 CN = servicebus.chinacloudapi.cn 的有效性,证书链信息在 SSL 协议的 server hello 消息中返回。

如果证书链中的某些中间证书没有安装在 web 应用实例上,web 应用需要发起额外的请求到 CA 服务器上下载中间证书并安装。当下面任何一种情况发生时,web 应用无法对 service bus 服务器证书建立信任的证书链,随后会报告上述的证书错误:

  1. 运行Service Bus客户端代码的实例和 CA 服务器之间存在网络问题,导致无法下载证书.
  2. 运行Service Bus客户端代码的实例无法成功安装证书,如权限问题等。

推荐在代码中强制使用 HTTPS 模式连接 Service Bus 服务,HTTPS 模式有不同的设计,因此会很大程度上避免上述错误发生。

示例代码如下:
string connectionString = "servicebus connection string";
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Https
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.TopicExists("TestTopic"))
{
    namespaceManager.CreateTopic("TestTopic");
}
TopicClient client = TopicClient.CreateFromConnectionString(connectionString, "TestTopic");
BrokeredMessage testMessage = new BrokeredMessage("test message");
testMessage.MessageId = "message id";
client.Send(testMessage);

另外,以上代码代码使用很旧的Service Bus SDK,强烈建议升级SDK代码,使用新的AMQP协议连接Service Bus

using Azure.Messaging.ServiceBus;
// the client that owns the connection and can be used to create senders and receivers
ServiceBusClient client;
// The Service Bus client types are safe to cache and use as a singleton for the lifetime
// of the application, which is best practice when messages are being published or read
// regularly.
//
// set the transport type to AmqpWebSockets so that the ServiceBusClient uses the port 443. 
// If you use the default AmqpTcp, you will need to make sure that the ports 5671 and 5672 are open
// TODO: Replace the <NAMESPACE-CONNECTION-STRING> and <QUEUE-NAME> placeholders
var clientOptions = new ServiceBusClientOptions()
{ 
   TransportType = ServiceBusTransportType.AmqpWebSockets
};
client = new ServiceBusClient("<NAMESPACE-CONNECTION-STRING>", clientOptions);

 

 

参考文档

添加将消息发送到队列的代码 : https://learn.microsoft.com/zh-cn/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues?tabs=passwordless#add-code-to-send-messages-to-the-queue

相关文章
|
27天前
|
Linux Shell 网络安全
【Azure 应用服务】如何来检查App Service上证书的完整性以及在实例中如何查找证书是否存在呢?
【Azure 应用服务】如何来检查App Service上证书的完整性以及在实例中如何查找证书是否存在呢?
|
27天前
【Azure Fabric Service】Service Fabric 托管群集通过 Connect-ServiceFabricCluster 连接时候报错 CertificatedNotMatched
【Azure Fabric Service】Service Fabric 托管群集通过 Connect-ServiceFabricCluster 连接时候报错 CertificatedNotMatched
|
27天前
|
JavaScript API 数据安全/隐私保护
【Azure Developer】Azure AD 注册应用的 OAuth 2.0 v2 终结点获取的 Token 解析出来依旧为v1, 这是什么情况!
【Azure Developer】Azure AD 注册应用的 OAuth 2.0 v2 终结点获取的 Token 解析出来依旧为v1, 这是什么情况!
|
29天前
|
缓存 API Windows
【服务总线 Azure Service Bus】Service Bus在使用预提取(prefetching)后出现Microsoft.Azure.ServiceBus.MessageLockLostException异常问题
【服务总线 Azure Service Bus】Service Bus在使用预提取(prefetching)后出现Microsoft.Azure.ServiceBus.MessageLockLostException异常问题
|
28天前
|
前端开发 API 网络架构
【Azure 应用服务】能否通过 Authentication 模块配置 Azure AD 保护 API 应用?
【Azure 应用服务】能否通过 Authentication 模块配置 Azure AD 保护 API 应用?
|
27天前
【Azure 服务总线】Azure门户获取ARM模板,修改Service Bus的TLS版本
【Azure 服务总线】Azure门户获取ARM模板,修改Service Bus的TLS版本
|
27天前
|
开发框架 安全 前端开发
【Azure 应用服务】应用代码需要客户端证书进行验证,部署到App Service后,如何配置让客户端携带证书呢?
【Azure 应用服务】应用代码需要客户端证书进行验证,部署到App Service后,如何配置让客户端携带证书呢?
|
27天前
|
Windows
【Azure 应用服务】应用代码中需要使用客户端证书访问服务接口,部署在应用服务后报错不能找到证书(Cannot find the X.509 certificate)
【Azure 应用服务】应用代码中需要使用客户端证书访问服务接口,部署在应用服务后报错不能找到证书(Cannot find the X.509 certificate)
|
27天前
|
网络安全
【Azure Service Bus】启用诊断日志来获取客户端访问Azure Service Bus的IP地址 [2024-03-26 实验结果失败]
【Azure Service Bus】启用诊断日志来获取客户端访问Azure Service Bus的IP地址 [2024-03-26 实验结果失败]
|
27天前
|
Linux 网络安全 API
【Azure 环境】当在Azure 环境中调用外部接口不通时,如何定位SSL Certificate Problem
【Azure 环境】当在Azure 环境中调用外部接口不通时,如何定位SSL Certificate Problem