Last active
December 29, 2019 03:46
-
-
Save gistlyn/d2ec590c89fb94f6ba5ed7a3ecd56640 to your computer and use it in GitHub Desktop.
C# Google protoc SSL GrpcServicesClient TodoWorld Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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