-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Deprecate service point for server certificate #4731
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
aik-jahoda
merged 7 commits into
dotnet:master
from
aik-jahoda:jajahoda/servicepointcert
Nov 4, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bdb991e
Deprecate service point for server certificate
48a931f
Unify obsoletion message with webrequest
5d448c9
Apply suggestions from code review
aik-jahoda e0a74f0
Update xml/System.Net.Http/HttpClientHandler.xml
aik-jahoda ea0b715
Update xml/System.Net.Http/HttpClientHandler.xml
aik-jahoda ed55594
Fix example path
a79eb72
Merge remote-tracking branch 'origin/jajahoda/servicepointcert' into …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...s/snippets/csharp/VS_Snippets_Misc/system.net.http.httpclienthandler-secure/cs/program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||
using System; | ||||||
using System.IO; | ||||||
using System.Net; | ||||||
using System.Net.Http; | ||||||
using System.Net.Security; | ||||||
using System.Security.Cryptography.X509Certificates; | ||||||
using System.Threading.Tasks; | ||||||
|
||||||
class HttpClientHandler_SecureExample | ||||||
{ | ||||||
// <Snippet1> | ||||||
static async Task Main() | ||||||
{ | ||||||
// Create an HttpClientHandler object and set to use default credentials | ||||||
HttpClientHandler handler = new HttpClientHandler(); | ||||||
|
||||||
// Set custom server validation callback | ||||||
handler.ServerCertificateCustomValidationCallback = ServerCertificateCustomValidation; | ||||||
|
||||||
// Create an HttpClient object | ||||||
HttpClient client = new HttpClient(handler); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// Call asynchronous network methods in a try/catch block to handle exceptions | ||||||
try | ||||||
{ | ||||||
HttpResponseMessage response = await client.GetAsync("https://docs.microsoft.com/"); | ||||||
|
||||||
response.EnsureSuccessStatusCode(); | ||||||
|
||||||
string responseBody = await response.Content.ReadAsStringAsync(); | ||||||
Console.WriteLine($"Read {responseBody.Length} characters"); | ||||||
} | ||||||
catch (HttpRequestException e) | ||||||
{ | ||||||
Console.WriteLine("\nException Caught!"); | ||||||
Console.WriteLine($"Message: {e.Message} "); | ||||||
} | ||||||
|
||||||
// Need to call dispose on the HttpClient and HttpClientHandler objects | ||||||
// when done using them, so the app doesn't leak resources | ||||||
handler.Dispose(); | ||||||
client.Dispose(); | ||||||
} | ||||||
|
||||||
private static bool ServerCertificateCustomValidation(HttpRequestMessage requestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslErrors) | ||||||
{ | ||||||
// It is possible inpect the certificate provided by server | ||||||
Console.WriteLine($"Requested URI: {requestMessage.RequestUri}"); | ||||||
Console.WriteLine($"Effective date: {certificate.GetEffectiveDateString()}"); | ||||||
Console.WriteLine($"Exp date: {certificate.GetExpirationDateString()}"); | ||||||
Console.WriteLine($"Issuer: {certificate.Issuer}"); | ||||||
Console.WriteLine($"Subject: {certificate.Subject}"); | ||||||
|
||||||
// Based on the custom logic it is possible to decide whether the client considers certificate valid or not | ||||||
Console.WriteLine($"Errors: {sslErrors}"); | ||||||
return sslErrors == SslPolicyErrors.None; | ||||||
} | ||||||
// </Snippet1> | ||||||
} |
9 changes: 9 additions & 0 deletions
9
...stem.net.http.httpclienthandler-secure/cs/system.net.http.httpclienthandler-secure.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<RootNamespace>dotnet_api_docs</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ippets_Misc/system.net.http.httpclienthandler/cs/system.net.http.httpclienthandler.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<RootNamespace>dotnet_api_docs</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.