如何在 MQTTnet 中使用客户端证书和密钥?
我想用MQTTNET复制此Python代码样本。
client = mqtt.Client(str(uuid4()))
current = dirname(__file__)
cerfile = join(current, "rcm_certchain_pem.cer")
keyfile = join(current, "rcm_pem_privkey.pkcs8")
context = create_default_context(Purpose.CLIENT_AUTH)
context.load_cert_chain(cerfile, keyfile)
client.tls_set_context(context)
client.tls_insecure_set(True)
client.connect("192.168.1.X", 1234)
我尝试了mqttnet。
var manager = new MqttFactory().CreateMqttClient();
var options = new MqttClientOptionsBuilder()
.WithCleanSession()
.WithClientId(Guid.NewGuid().ToString())
.WithTcpServer(address, port)
.WithTls(new MqttClientOptionsBuilderTlsParameters()
{
AllowUntrustedCertificates = true,
Certificates = new List<X509Certificate>
{
new X509Certificate2("Assets/rcm_certchain_pem.cer"),
new X509Certificate2("Assets/rcm_pem_privkey.pkcs8")
},
UseTls = true,
})
.WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
.Build();
await manager.ConnectAsync(options, CancellationToken.None);
我总是有这个与加密相关的错误。
<Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Cannot find the requested object.
I would like to replicate this Python code sample with MQTTnet.
client = mqtt.Client(str(uuid4()))
current = dirname(__file__)
cerfile = join(current, "rcm_certchain_pem.cer")
keyfile = join(current, "rcm_pem_privkey.pkcs8")
context = create_default_context(Purpose.CLIENT_AUTH)
context.load_cert_chain(cerfile, keyfile)
client.tls_set_context(context)
client.tls_insecure_set(True)
client.connect("192.168.1.X", 1234)
I tried this with MQTTnet.
var manager = new MqttFactory().CreateMqttClient();
var options = new MqttClientOptionsBuilder()
.WithCleanSession()
.WithClientId(Guid.NewGuid().ToString())
.WithTcpServer(address, port)
.WithTls(new MqttClientOptionsBuilderTlsParameters()
{
AllowUntrustedCertificates = true,
Certificates = new List<X509Certificate>
{
new X509Certificate2("Assets/rcm_certchain_pem.cer"),
new X509Certificate2("Assets/rcm_pem_privkey.pkcs8")
},
UseTls = true,
})
.WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
.Build();
await manager.ConnectAsync(options, CancellationToken.None);
I always have this crypto related error.
<Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Cannot find the requested object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MQTTNET Wiki(这不是您想要的)中记录的示例假定您有一个PFX(PKCS12)文件...它与您的键 +证书的内容相同,只是包装的方式不同。
我尝试的是以编程方式创建PFX,将其添加到X509Certificate2的列表中,然后将其分配给MQTTClientOptionsBuilderTlsParameters.Certificates。笨拙,但它起作用。代码样本:
The example documented in the MQTTnet Wiki (which is not what you want) presumes you have a PFX (PKCS12) file ... which is the same content as your Key + Cert, just packaged differently.
What I tried was to create the PFX programmatically, add it to a List of X509Certificate2 and then assign THAT to to MqttClientOptionsBuilderTlsParameters.Certificates. Clunky, but it works. Code sample: