Search code examples
c#serilogappsettings

How can I disable server certificate validation in Serilog using appsettings.json?


When using the email sink in serilog I get the error

Failed to send email: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

Through code, I can supply on the EmailConnectionInfo:

ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true

But I am using appsettings.json to load the Serilog configuration.

Is it possible to supply it through appsettings.json somehow?

I tried (stripped sensitive fields in the below example) without luck, same error:

"Serilog": {
    "Using": [ "Serilog.Sinks.Email" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Email",
        "Args": {
          "connectionInfo": {
            "FromEmail": "",
            "ToEmail": "",
            "MailServer": "",
            "EmailSubject": "",
            "serverCertificateValidationCallback": "(s, cert, chain, sslPolicyErrors) => true"
          }
        }
      }
    ]
  }

Solution

  • At the moment, it is not possible to configure the ServerCertificateValidationCallback via configuration. You'll have to configure it via code.

    You can open an issue in the Serilog.Settings.Configuration repository to track a feature request for configuring callbacks via configuration files and/or open an issue in the Serilog.Sinks.Email repository to see if a workaround can be implemented in the future.