Files
BQP/srcs/RobotNet10/Commons/RobotNet10.MqttConnection/MQTTClientCertificatesProvider.cs
2026-07-13 09:25:40 +07:00

22 lines
909 B
C#

using MQTTnet;
using System.Security.Cryptography.X509Certificates;
namespace RobotNet10.MqttConnection;
public class MQTTClientCertificatesProvider(string? CerPath, string? KeyPath) : IMqttClientCertificatesProvider
{
public X509CertificateCollection? GetCertificates()
{
if (!string.IsNullOrEmpty(CerPath) && !string.IsNullOrEmpty(KeyPath))
{
if (File.Exists(CerPath) && File.Exists(KeyPath))
{
var cert = X509Certificate2.CreateFromPem(File.ReadAllText(CerPath), File.ReadAllText(KeyPath));
var pfxBytes = cert.Export(X509ContentType.Pfx);
var pfxCert = X509CertificateLoader.LoadPkcs12(pfxBytes, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
return [pfxCert];
}
}
return null;
}
}