我是 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 框架客户端,典型代码是
Channel channel = new Channel("localhost", 10042, ChannelCredentials.Insecure);
唯一的选择似乎是Insecure
最后,对于服务器,是否可以使用自签名证书?