---
# System prepended metadata

title: gRPC オレオレ証明書を使用しているホストへの接続 2021-10-07
tags: [オレオレ証明書, gRPC, 接続]

---

---
lang: ja-jp
breaks: true
---
# gRPC オレオレ証明書を使用しているホストへの接続 2021-10-07

> The SSL connection could not be established
> https://stackoverflow.com/questions/52939211/the-ssl-connection-could-not-be-established

> 信頼されていないか無効な証明書で gRPC サービスを呼び出す
> https://docs.microsoft.com/ja-jp/aspnet/core/grpc/troubleshoot#call-a-grpc-service-with-an-untrustedinvalid-certificate

```csharp=
var socketsHttpHandler = new SocketsHttpHandler
{
    SslOptions = new SslClientAuthenticationOptions
    {
        RemoteCertificateValidationCallback = RemoteCertificateValidationCallback
    }
};
var canelOptions = new GrpcChannelOptions
{
    HttpHandler = socketsHttpHandler
};
GrpcChannel channel = GrpcChannel.ForAddress(url, canelOptions);
```

:::info
昔の書き方
```csharp=
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = RemoteCertificateValidationCallback;
var canelOptions = new GrpcChannelOptions()
{
    HttpHandler = httpClientHandler
};
GrpcChannel channel = GrpcChannel.ForAddress(url, canelOptions);
```
:::

```csharp=
public static bool RemoteCertificateValidationCallback(
    object sender, 
    X509Certificate certificate, 
    X509Chain chain, 
    SslPolicyErrors sslPolicyErrors
    )
{
    // 証明書関連のエラーを全て無視して許可する。
    return true;
}
```

## 以下の方法ではダメ

```csharp=
// ↓これではだめ。
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
```

###### tags: `gRPC` `オレオレ証明書` `接続`
