diff --git a/snippets/csharp/System.Net.Http/WinHttpHandler/Project.csproj b/snippets/csharp/System.Net.Http/WinHttpHandler/Project.csproj new file mode 100644 index 00000000000..c99f5065527 --- /dev/null +++ b/snippets/csharp/System.Net.Http/WinHttpHandler/Project.csproj @@ -0,0 +1,12 @@ + + + + Library + net9.0 + + + + + + + \ No newline at end of file diff --git a/snippets/csharp/System.Net.Http/WinHttpHandler/program.cs b/snippets/csharp/System.Net.Http/WinHttpHandler/program.cs new file mode 100644 index 00000000000..47d739424ca --- /dev/null +++ b/snippets/csharp/System.Net.Http/WinHttpHandler/program.cs @@ -0,0 +1,29 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Net.Security; + +class WinHttpHandler_SecureExample +{ + static void Main() + { + if (!OperatingSystem.IsWindows()) + { + Console.WriteLine("This example requires Windows."); + return; + } + // + var handler = new WinHttpHandler(); + handler.ServerCertificateValidationCallback = (httpRequestMessage, certificate, chain, sslPolicyErrors) => + { + if (sslPolicyErrors == SslPolicyErrors.None) + { + // TODO: Implement additional custom certificate validation logic here. + return true; + } + // Do not allow this client to communicate with unauthenticated servers. + return false; + }; + // + } +} diff --git a/xml/System.Net.Http/WinHttpHandler.xml b/xml/System.Net.Http/WinHttpHandler.xml index 13d3f0dfc25..6f311881e4c 100644 --- a/xml/System.Net.Http/WinHttpHandler.xml +++ b/xml/System.Net.Http/WinHttpHandler.xml @@ -740,13 +740,20 @@ When this property is set to `true`, all HTTP redirect responses from the server Gets or sets a callback method to validate the server certificate. This callback is part of the SSL handshake. - The callback should return if the server certificate is considered valid and the request should be sent. Otherwise, return . + The callback should return if the server certificate is considered valid and the request should be sent. Otherwise, returns . value returned by this delegate determines whether the authentication is allowed to succeed. + +## Examples + +The following code example implements the callback. If there are validation errors, this method returns `false` preventing communication with the unauthenticated server. Otherwise, it allows for additional validation and return `true` if the certificate is valid. + + :::code language="csharp" source="~/snippets/csharp/System.Net.Http/WinHttpHandler/program.cs" id="Snippet1"::: ]]>