Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active December 29, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/d2ec590c89fb94f6ba5ed7a3ecd56640 to your computer and use it in GitHub Desktop.
Save gistlyn/d2ec590c89fb94f6ba5ed7a3ecd56640 to your computer and use it in GitHub Desktop.
C# Google protoc SSL GrpcServicesClient TodoWorld Example
using System;
using System.Linq;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
namespace TodoWorld
{
class Program
{
static async Task Main(string[] args)
{
var client = new GrpcServices.GrpcServicesClient(
GrpcChannel.ForAddress("https://todoworld.servicestack.net:50051", new GrpcChannelOptions {
HttpClient = new System.Net.Http.HttpClient(new System.Net.Http.HttpClientHandler {
ClientCertificates = { new X509Certificate2("grpc.crt") },
ServerCertificateCustomValidationCallback = (req, cert, certChain, sslErrors) =>
cert.SubjectName.RawData.SequenceEqual(cert.IssuerName.RawData) && // self-signed
cert.GetNameInfo(X509NameType.DnsName, forIssuer:false) == "todoworld.servicestack.net" &&
(sslErrors & ~SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.None // only this
})
}));
var response = await client.GetHelloAsync(new Hello { Name = "gRPC C#" });
Console.WriteLine(response.Result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment