解决Azure 消息队列ServiceBus提示证书不信任无权限的问题

笔者在C# 消息队列-Microsoft Azure service bus 服务总线中使用Azure消息队列,最近偶尔会遇到请求数据不入队列的问题,查找日志,问题如下:

异常:X.509 certificate CN=servicebus.chinacloudapi.cn, 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, 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.

问题显示使用的证书有一个无法验证的信任链。更换证书或改变certificatevalidationmode。无法将证书链构建到受信任的根权限。

问题出现是因为我在创建连接时没有对ConnectivityMode 做预先设置

如果代码中不对 ConnectivityMode 做预先设置,service bus 客户端( web 应用)默认使用了 AutoDetect 模式 连接 service bus 服务。 AutoDetect 会优先使用 TCP 连接模式。由于 TCP 连接模式也是加密的,所以客户端需要首先验证 service bus 服务器证书 CN = servicebus.chinacloudapi.cn 的有效性,证书链信息在 SSL 协议的 server hello 消息中返回。

如果证书链中的某些中间证书没有安装在 web 应用实例上,web 应用需要发起额外的请求到 CA 服务器上下载中间证书并安装。

修改代码如下:

//最好设置连接模式为Https,默认是Tcp。如果是Tcp的话,放到云端WEB应用上会报错
 ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Https;
//创建连接
var client = QueueClient.CreateFromConnectionString(connectionString, queueName);

 

posted @ 2017-11-16 15:06  一指流砂~  阅读(662)  评论(0编辑  收藏  举报