简体   繁体   English

如何从 Xamarin.Forms(iOS 和 Android)调用自签名 gRPC 服务

[英]How to call self-signed gRPC service from Xamarin.Forms (iOS and Android)

I'm trying to call a gRPC service built using ASP.NET Core on .NET 5 from a Xamarin.Forms application.我正在尝试从 Xamarin.Z645024ZZ6683CAE61 应用程序调用 .NET 5 上使用 ASP.NET 核心构建的 gRPC 服务。

I'm using Grpc.AspNetCore 2.35.0 on the server side and Grpc.Net.Client 2.35.0 within the app library (.NETStandard 2.1).我在服务器端使用 Grpc.AspNetCore 2.35.0,在应用程序库(.NETStandard 2.1)中使用 Grpc.Net.Client 2.35.0。

When trying to call a service from the client, I get an exception:尝试从客户端调用服务时,出现异常:

Exception caught: MyException: An unknown error happened
---> Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: The SSL connection could not be established, see inner exception. AuthenticationException: Authentication failed, see inner exception. TlsException: CertificateUnknown", DebugException="System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> Mono.Security.Interface.TlsException: CertificateUnknown
  at Mono.AppleTls.AppleTlsContext.EvaluateTrust () [0x000bf] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/Mono.AppleTls/AppleTlsContext.cs:306 
[… SNIP …]

I assume this has to do with the self signed certificate.我认为这与自签名证书有关。 I'm using the generated dev certificate created using dotnet dev-certs https tool.我正在使用使用dotnet dev-certs https工具创建的生成的开发证书。

I tried to add a custom handler to the created channel as suggested here :我尝试按照此处的建议将自定义处理程序添加到创建的通道:

GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
{
    HttpHandler = new HttpClientHandler
    {
        ServerCertificateCustomValidationCallback =
            HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
    }
});

And also the other suggestion:还有另一个建议:

AppContext.SetSwitch(
    "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
GrpcChannel.ForAddress("http://localhost:5000");

But both approaches result in another exception (presumably because this will use an older HttpClientHandler implementation that doesn't support HTTP/2.0):但是这两种方法都会导致另一个异常(可能是因为这将使用不支持 HTTP/2.0 的旧 HttpClientHandler 实现):

Exception caught: MyException: An unknown error happened
---> Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Request protocol 'HTTP/1.1' is not supported.")

I also tried to circumvent the self-signed certificate by creating a custom CA plus certificate and installing that root certificate in the iOS simulator.我还尝试通过创建自定义 CA plus 证书并将该根证书安装在 iOS 模拟器中来规避自签名证书。 But the same result occurred.但同样的结果发生了。 I'm not sure if it would have made a difference on Android, because I was not able to install the root certificate on the emulator.我不确定它是否会对 Android 产生影响,因为我无法在模拟器上安装根证书。

How can I use gRPC with Xamarin.Forms against a development server?如何将 gRPC 与 Xamarin.Forms 一起用于开发服务器?

I've fixed he above exception changing my implementation to match this: https://techblog.livongo.com/implementing-grpc-in-a-xamarin-mobile-project/ block.我已经修复了上述异常,更改了我的实现以匹配此: https://techblog.livongo.com/implementing-grpc-in-a-xamarin-mobile-project/块。

In more details:详细信息:

  • I've added references to Grpc.Core, Grpc.Tools, Grpc.Net.Common, Grpc.Net.Client, Grpc.Core.Api;我添加了对 Grpc.Core、Grpc.Tools、Grpc.Net.Common、Grpc.Net.Client、Grpc.Core.Api 的引用;

  • I've added this code when creating the Channel:我在创建频道时添加了这段代码:

     var credentials = CallCredentials.FromInterceptor((context, metadata) => Task.CompletedTask); return new Channel("<your domai>:<port>", ChannelCredentials.Create(new SslCredentials(), credentials));

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

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