c# - MQTTnet 客户端认证
问题描述
我有以下错误,当尝试使用以下选项连接到服务器上的蚊子代理时,我收到关于私钥的以下错误,但我正在使用服务器,crt 来验证没有分配私钥的服务器。
代码:
var serverCertificate = new X509Certificate2("ca.crt");
Debug.WriteLine("+++++" + serverCertificate.HasPrivateKey);
var mqttClient = new MqttFactory().CreateMqttClient();
// Create TCP based options using the builder.
var options = new MqttClientOptionsBuilder()
.WithClientId("foo")
.WithTcpServer("192.168.1.126", 8883)
.WithCredentials("myusername", "mypassword")
//.WithTls(true)
.WithTls(true, false, false, serverCertificate.Export(X509ContentType.Cert))
.WithCleanSession()
.WithProtocolVersion(MqttProtocolVersion.V311)
.WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
.WithCommunicationTimeout(TimeSpan.FromSeconds(30))
.Build();
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var connect = await mqttClient.ConnectAsync(options);
错误
任何帮助将不胜感激,谢谢!
解决方案
我将我的证书存储在
/data/user/0/com.companyname.name/files
使用此命令创建 pfx 文件
openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx
使用.pfx
代替.crt
.pfx
从本地文件中读取,byte[]
将其用作证书
推荐阅读
- javascript - 在开关中投射类型
- git - 父模块中的 git status 未显示子模块中的新提交
- apache - 反向代理尝试将子目录重定向到 Symfony 特定路由
- python - 在 Python 中序列化/反序列化类对象的最佳方法是什么?
- java - 并行流修改的 ArrayList 的大小
- c# - 为什么更改元素属性时此 Xamarin CollectionView 不更新 UI?
- windows - 启动 kafka 服务器时出现“java.nio.channels.UnresolvedAddressException”
- mysql - 多个表中的最大函数
- asp.net-core - 元标记中的内容
- docker - docker 在镜像中创建的文件没有反映在 docker compose 运行的容器中