Skip to content

Commit bc7a520

Browse files
ManickaPantonfirsovgewarren
authored
[HTTP] Example for WinHttpHandler.ServerCertificateValidationCallback (#11463)
* Improve example for ServerCertificateValidationCallback * Update snippets/csharp/System.Net.Http/WinHttpHandler/program.cs Co-authored-by: Anton Firszov <[email protected]> * Update xml/System.Net.Http/WinHttpHandler.xml Co-authored-by: Genevieve Warren <[email protected]> * Fixed sample * Change description with changed example --------- Co-authored-by: Anton Firszov <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 2aabd6b commit bc7a520

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="9.0.6" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Net.Security;
5+
6+
class WinHttpHandler_SecureExample
7+
{
8+
static void Main()
9+
{
10+
if (!OperatingSystem.IsWindows())
11+
{
12+
Console.WriteLine("This example requires Windows.");
13+
return;
14+
}
15+
// <Snippet1>
16+
var handler = new WinHttpHandler();
17+
handler.ServerCertificateValidationCallback = (httpRequestMessage, certificate, chain, sslPolicyErrors) =>
18+
{
19+
if (sslPolicyErrors == SslPolicyErrors.None)
20+
{
21+
// TODO: Implement additional custom certificate validation logic here.
22+
return true;
23+
}
24+
// Do not allow this client to communicate with unauthenticated servers.
25+
return false;
26+
};
27+
// </Snippet1>
28+
}
29+
}

xml/System.Net.Http/WinHttpHandler.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,20 @@ When this property is set to `true`, all HTTP redirect responses from the server
740740
</ReturnValue>
741741
<Docs>
742742
<summary>Gets or sets a callback method to validate the server certificate. This callback is part of the SSL handshake.</summary>
743-
<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>
743+
<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>
744744
<remarks>
745745
<format type="text/markdown"><![CDATA[
746746
747747
## Remarks
748748
The default value is `null`. If this property is `null`, the server certificate is validated using standard well-known certificate authorities.
749749
750+
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.
751+
752+
## Examples
753+
754+
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.
755+
756+
:::code language="csharp" source="~/snippets/csharp/System.Net.Http/WinHttpHandler/program.cs" id="Snippet1":::
750757
]]></format>
751758
</remarks>
752759
</Docs>

0 commit comments

Comments
 (0)