Skip to content

[HTTP] Example for WinHttpHandler.ServerCertificateValidationCallback #11463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions snippets/csharp/System.Net.Http/WinHttpHandler/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="9.0.6" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions snippets/csharp/System.Net.Http/WinHttpHandler/program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
// <Snippet1>
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;
};
// </Snippet1>
}
}
9 changes: 8 additions & 1 deletion xml/System.Net.Http/WinHttpHandler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,20 @@ When this property is set to `true`, all HTTP redirect responses from the server
</ReturnValue>
<Docs>
<summary>Gets or sets a callback method to validate the server certificate. This callback is part of the SSL handshake.</summary>
<value>The callback should return <see langword="true" /> if the server certificate is considered valid and the request should be sent. Otherwise, return <see langword="false" />.</value>
<value>The callback should return <see langword="true" /> if the server certificate is considered valid and the request should be sent. Otherwise, returns <see langword="false" />.</value>
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks
The default value is `null`. If this property is `null`, the server certificate is validated using standard well-known certificate authorities.

The delegate's `sslPolicyErrors` argument contains any certificate errors returned by SSPI while authenticating the server. The <xref:System.Boolean> 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":::
]]></format>
</remarks>
</Docs>
Expand Down