有没有人设法通过 SSL 使用 .NET Core 2.0 连接到 AmazonMQ?
使用 Apache.NMS.ActiveMQ.Core 包进行连接时出现以下错误:
System.NotSupportedException: The requested security protocol is not supported.
at System.Net.SecurityProtocol.ThrowOnNotAllowed(SslProtocols protocols, Boolean allowNone)
at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
at Apache.NMS.ActiveMQ.Transport.Tcp.SslTransport.CreateSocketStream()
at Apache.NMS.ActiveMQ.Transport.Tcp.TcpTransport.Start()
at Apache.NMS.ActiveMQ.Transport.WireFormatNegotiator.Start()
at Apache.NMS.ActiveMQ.Connection.CheckConnected()
at Apache.NMS.ActiveMQ.Connection.CreateActiveMQSession(AcknowledgementMode ackMode)
at SendToMQ.Program.SendMessage() in C:\GITLab\POC\SendToMQ\SendToMQ\SendToMQ\Program.cs:line 40
at SendToMQ.Program.Main(String[] args) in C:\GITLab\POC\SendToMQ\SendToMQ\SendToMQ\Program.cs:line 15
使用以下代码:
string url = "ssl://url:61617";
string userName = "name";
string password = "pw";
string payLoad = "This is a test.";
IConnectionFactory factory = new ConnectionFactory(new Uri(url));
using (IConnection connection = factory.CreateConnection(userName, password))
{
using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
{
IDestination destination = Apache.NMS.Util.SessionUtil.GetDestination(session, "TestTopic");
using (IMessageProducer producer = session.CreateProducer(destination))
{
connection.Start();
ITextMessage request = session.CreateTextMessage(payLoad);
request.Properties["id"] = 123;
producer.Send(request);
}
}
}
提前致谢。
最佳答案
您必须创建一个 SslFactory 并将 SslOption 专门设置为 Tls。
否则,NMS 默认为 SslProtocol.Default(在 Apache.NMS.ActiveMQ.Transport.Tcp.SslTransport.GetAllowedProtocol() 中),即 Ssl3。
String uri = "ssl://yourinstance:61617?nms.AsyncSend=true";
ITransportFactory sslTransportFactory = new SslTransportFactory();
((SslTransportFactory)sslTransportFactory).SslProtocol = "Tls";
ITransport transport = sslTransportFactory.CreateTransport(new Uri(uri));
Connection connection = new Connection(new Uri(uri), transport, new IdGenerator())
{
UserName = "user",
Password = "pwd"
};
ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
IDestination dest = session.GetQueue("my_queue");
IMessageProducer producer = session.CreateProducer(dest);
producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
var msg = session.CreateTextMessage("Some msg");
producer.Send(msg);
connection.Close();