如何在.net中使用SSL设置grpc

时间:2017-05-26 14:16:45

标签: .net ssl protocol-buffers grpc

如何在.net中为grpc启动并运行服务器端身份验证? 一直在生成证书并尝试不同的设置,我感到很累。没有SSL可以正常工作。

这是我做的最后一次尝试,给我错误

Status(StatusCode=Unavailable, Detail="Connect Failed"

像这样的证据

openssl genrsa -out server.key 2048
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650

服务器

        const int port = 50052;

        var serverimpl= new Server
        {
            Services =
            {
                TestServer.BindService(new TestServerImpl())
            },
            Ports =
            {
                new ServerPort("localhost", port, new SslServerCredentials(new List<KeyCertificatePair>
                {
                    new KeyCertificatePair
                    (
                       File.ReadAllText(@"server.crt"),
                       File.ReadAllText(@"server.key")
                    )
                }))
            }
        };
        serverimpl.Start();

        Console.WriteLine("Test server listening on port " + port);
        Console.WriteLine("Press any key to stop the server...");
        Console.ReadKey();

        serverimpl.ShutdownAsync().Wait();

客户

        var channel = new Channel("127.0.0.1", 50052, new SslCredentials(File.ReadAllText("server.crt")));
        var client = new TestServer.TestServiceClient(channel);



        var result = client.TestCall(new Query
        {    
            From = DateTime.UtcNow.AddDays(-10).Ticks,
            To = DateTime.UtcNow.Ticks
        });

        channel.ShutdownAsync().Wait();

1 个答案:

答案 0 :(得分:0)

原来我必须阅读open-ssl用法,这将生成工作证书,代码很好。

openssl req \
   -newkey rsa:2048 -nodes -keyout domain.key \
   -x509 -days 365 -out domain.crt

一些易于理解的操作方法

https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs