【发布时间】:2019-07-09 15:21:13
【问题描述】:
我正在开发一个 Xamarin 应用程序,我正在与服务器建立连接。服务器代码目前对我来说是一个黑匣子,我只有文档。
但是,由于服务器切换到 TLS1.2,我正在尝试使用 .NET 的 SslStream 在我的应用程序上进行身份验证。我确保两者都使用相同的证书。 证书是自签名的。
每当我尝试执行 AuthenticateAsClient 时,都会出现以下异常:
Mono.Security.Interface.TlsException: Unknown Secure Transport error `PeerHandshakeFail'.
这是我的代码的一部分:
using (var stream = new SslStream(new NetworkStream(mainSocket), false, new RemoteCertificateValidationCallback(ValidateServerCertificate)))
{
try
{
stream.AuthenticateAsClient(ServerIpAdressServer, GetX509CertificateCollection(), System.Security.Authentication.SslProtocols.Tls12, false);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
(ValidateServerCertificate 总是返回 true)
这是我获得证书的方法:
public static X509CertificateCollection GetX509CertificateCollection()
{
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MyClass)).Assembly;
X509CertificateCollection collection1;
using (MemoryStream ms = new MemoryStream())
{
assembly.GetManifestResourceStream("namespace.cert.pem").CopyTo(ms);
X509Certificate2 certificate1 = new X509Certificate2(ms.ToArray());
collection1 = new X509CertificateCollection();
collection1.Add(certificate1);
}
return collection1;
}
提前致谢!
【问题讨论】:
-
可能客户端与TLS1.2中的服务器不兼容。
-
@JuniorJiang-MSFT 你知道有什么原因吗?通常xamarin iOS应该兼容TLS1.2吧?
-
好吧,我已经分享了答案。
标签: c# xamarin tls1.2 .net-standard sslstream