MQTT 프로토콜이 새로 도입되었습니다. MQTT 서버를 검색했을 때 Mosquitto 브로커가 가장 많이 사용 된 브로커 중 하나이므로 브로커를 사용하기 시작했습니다. C#/.NET에서 MQTT 클라이언트를 개발해야하며 M2Mqtt 프로젝트 만 발견했다. C# MQTT client example.M2Mqtt.MqttClient를 TLS가 적용된 Mosquitto 브로커에 연결할 때 AuthenticationException이 발생합니다
나는 install Mosquitto broker on Windows 10과 change the access control list using topics으로 관리했습니다. MqttClient
사용 사용자 이름으로 브로커에 연결하고 주제를 구독하고 다음 코드로 게시 할 수 있습니다.
연결 :
byte result = this.mqttClient.Connect(Guid.NewGuid().ToString(), username, string.Empty);
구독 :
this.mqttClient.Subscribe(new string[] { topic }, new byte[] { 2 });
가 게시는 :
ushort result = this.mqttClient.Publish(topic, message, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
은 이제 통신에 보안을 추가 할 수 있습니다. 나는 steps to create the CA certificate, the server key and server certificate (인증서를 두 번 만들었습니다)을 따랐습니다. 나는 링크에 설명 된대로 mosquito.conf
파일 변경 :
port 8883
cafile C:\mosquitto\certs\ca.crt
certfile C:\mosquitto\certs\server.crt
keyfile C:\mosquitto\certs\server.key
이 필요한 경우 나도 몰라,하지만 나는 these steps 다음 신뢰할 수있는 루트 인증서에 ca.crt
을 추가했습니다.
//this.mqttClient = new MqttClient(brokerAddress);
X509Certificate caCertificate = new X509Certificate("ca.crt");
this.mqttClient = new MqttClient(brokerAddress, 8883, true, caCertificate, null, MqttSslProtocols.TLSv1_0);
ca.crt
파일의 사본이 .exe
파일의 동일한 폴더에 있습니다
uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException
을 : 예외가 브로커에 연결- [내부 예외]
System.Security.Authentication.AuthenticationException
: 원격 인증서가 검증 절차에 따라 유효하지 않습니다.
내가 누락 된 부분을 알고 계십니까?
그리고 어떻게 .crt 인증서를 .pfx로 내 보냅니 까? – Jon
@Jon 먼저 인증서를 로컬 인증서에 설치 한 다음> 모든 작업> 내보내기를 마우스 오른쪽 버튼으로 클릭하고 단계를 따르고 요청시 .pfx를 선택하십시오. – Mason