Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
25 changes: 25 additions & 0 deletions snippets/csharp/System.Net.Http/WinHttpHandler/program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;

class WinHttpHandler_SecureExample
{
static async Task Main()
{
// <Snippet1>
var handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = (httpRequestMessage, certificate, chain, sslPolicyErrors)

Check failure on line 13 in snippets/csharp/System.Net.Http/WinHttpHandler/program.cs

View workflow job for this annotation

GitHub Actions / snippets-build

C:\a\dotnet-api-docs\dotnet-api-docs\snippets\csharp\System.Net.Http\WinHttpHandler\program.cs(13,112): error CS1002: ; expected [C:\a\dotnet-api-docs\dotnet-api-docs\snippets\csharp\System.Net.Http\WinHttpHandler\Project.csproj]
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;

Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

// Do not allow this client to communicate with unauthenticated servers.
return false;
}
// </Snippet1>
}
}
6 changes: 6 additions & 0 deletions xml/System.Net.Http/WinHttpHandler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,12 @@ When this property is set to `true`, all HTTP redirect responses from the server
## 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 displays them and returns `false`, which prevents communication with the unauthenticated server.

:::code language="csharp" source="~/snippets/csharp/System.Net.Http/WinHttpHandler/program.cs" id="Snippet1":::
]]></format>
</remarks>
</Docs>
Expand Down
Loading