【问题标题】:gRPC server .net framework - how to configure to use a TLS certificategRPC 服务器 .net 框架 - 如何配置为使用 TLS 证书
【发布时间】:2020-11-27 00:50:31
【问题描述】:

我是 gRPC 的新手,正在探索我的 .net 框架服务器中的通信选项。

由于服务器项目的年龄和规模,可能需要一段时间才能将这些项目移植到 .net 5+,但我希望允许较新的 .net 5+ 客户端连接,因此想要替换具有最新内容的现有服务器 WCF。

我的 WCF 一直使用 Windows 身份验证进行加密,由于跨域问题,我迫不及待想要摆脱它,所以想使用 TLS。

我发现很难找到如何做到这一点的示例,尤其是在 .net 框架中,因为大多数示例都是 .net 核心。

这是一些典型的(ish)示例代码:

     static async Task Main()
        {
            const int port = 10042;
            
            var server = new Grpc.Core.Server
            {
                Ports = { new ServerPort("localhost", 10042, ServerCredentials.Insecure) }
            };
            server.Services.AddCodeFirst<ICalculator>(new Calculator());
            server.Services.AddCodeFirst<ITimeService>(new TimeService());
            server.Start();

            Console.WriteLine("server listening on port " + port);
            Console.ReadKey();

            await server.ShutdownAsync();
     }

忽略AddCodeFirst,这些是其他一些实验,但是每个示例似乎都使用ServerCredentials.Insecure,并且他们都说不要在生产中使用它(我不想这样做),但令人沮丧的是他们没有显示如何使用 TLS 证书。我该怎么做?

另外,对于 .net framework clients,典型的代码是

Channel channel = new Channel("localhost", 10042, ChannelCredentials.Insecure);

唯一的选项似乎是Insecure

最后,对于服务器,是否可以使用自签名证书?

【问题讨论】:

    标签: c# .net grpc grpc-dotnet


    【解决方案1】:

    要将 TLS 与自签名证书一起使用,您需要传递 SslServerCredentials(keyCertPair, caRoots, clientCertRequestType) 的实例而不是 ServerCredentials.Insecure,如本示例所示 请参阅旧版 gRPC 身份验证谈话中的 https://github.com/jtattermusch/grpc-authentication-kubernetes-examples/blob/979345cca801b71eba9f8ffc67e13bf57c33a211/greeter-server/Program.cs#L43 https://github.com/jtattermusch/grpc-authentication-kubernetes-examples

    在客户端,您需要传递相应的 CA 根(另见相同示例)。

    【讨论】:

    • 谢谢你,正是我想要的!
    猜你喜欢
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多