我想用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.
型
1条答案
按热度按时间w8f9ii691#
MQTTnet Wiki中记录的示例(这不是您想要的)假设您有一个PFX(PKCS12)文件.
我尝试以编程方式创建PFX,将其添加到X509Certificate2的列表中,然后将THAT分配给MqttClientOptionsBuilderTlsParameters.Certificates.Clunky,但它可以工作。代码示例:
字符串