简体   繁体   English

忽略.Net Standard 2.0中的SSL证书

[英]Ignore SSL certificate in .Net Standard 2.0

I am creating a Xamarin Cross Platform Application in it, I have replaced the Portable Class Library with .Net Standard 2.0 . 我正在其中创建一个Xamarin跨平台应用程序,已用.Net Standard 2.0替换了可移植类库。 From this project we are making a call to web service hosted on azure service fabric cluster (this service is hosted using self signed certificate).I am getting the following error while making a connection request - 从这个专案中,我们正在呼叫azure服务架构丛集(这个服务是使用自我签署的凭证代管)所代管的网路服务。提出连接要求时,出现以下错误-

InnerException {System.Runtime.InteropServices.COMException (0x80072F0D): The text associated with this error code could not be found. InnerException {System.Runtime.InteropServices.COMException(0x80072F0D):找不到与此错误代码关联的文本。

The certificate authority is invalid or incorrect 证书颁发机构无效或不正确

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpHandlerToFilter.d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.DiagnosticsHandler.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClientHandler.d__111.MoveNext()} System.Exception {System.Runtime.InteropServices.COMException} 在System.Net.Http.HttpHandlerToFilter.d__15.MoveNext()处的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)-从上次引发异常的位置开始的堆栈跟踪-在System.Runtime处System.Net.Http.DiagnosticsHandler.d__2.MoveNext()处System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)处的ExceptionServices.ExceptionDispatchInfo.Throw()-从上次引发异常的位置开始的堆栈跟踪结束- -在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在System.Net.Http.HttpClientHandler.d__111.MoveNext()的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处System.Exception {System.Runtime .InteropServices.COMException}

I think the self-signed SSL causing the problem, in general in order to bypass certificate we use following code snippet 我认为自签名SSL会引起问题,通常为了绕过证书,我们使用以下代码段

ServicePointManager.ServiceCertificateValidationCallback += (o, c, ch, er) => true;

And it works for Console Application targeting .Net framework 4.6.1 but this code doesn't seem to work for .Net Standard 2.0 and .Net Core 2.0 apps. 它适用于面向.Net Framework 4.6.1的控制台应用程序,但此代码似乎不适用于.Net Standard 2.0和.Net Core 2.0应用程序。 Do we have to do something different in order to bypass certificates in .Net Standard and .Net Core apps. 为了绕过.Net Standard和.Net Core应用程序中的证书,我们是否需要做一些不同的事情。 Following is the sample code area that is producing this error 以下是产生此错误的示例代码区域

IHubProxy _hub;
string url = @"https://www.xyz:com:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("MyHub");
connection.Start().Wait();

You can simply write this in your platform specific projects like in Main Activity / App Delegate: 您可以简单地在特定于平台的项目中编写此代码,例如在Main Activity / App Delegate中:

ServicePointManager.ServerCertificateValidationCallback +=
     (sender, cert, chain, sslPolicyErrors) => true;

ServicePointManager is not available in .NET Core, but you should be able to set a custom delegate using the HttpClientHandler.ServerCertificateCustomValidationCallback property using System.Net.Http 4.1+. ServicePointManager在.NET Core中不可用,但是您应该能够使用System.Net.Http 4.1+使用HttpClientHandler.ServerCertificateCustomValidationCallback属性设置自定义委托。

See Add support for custom validation of server SSL Certificate to HttpClient API and System.Net.Http 4.1 Contract Revisions on dotnet/corefx GitHub. 请参阅在dotnet / corefx GitHub上为HttpClient APISystem.Net.Http 添加对服务器SSL证书的自定义验证的支持 4.1合同修订

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM