Skip to content

Instantly share code, notes, and snippets.

@zivkan
Last active January 6, 2024 16:33
Show Gist options
  • Save zivkan/5291f507c8c5724d41a18357b7afcd30 to your computer and use it in GitHub Desktop.
Save zivkan/5291f507c8c5724d41a18357b7afcd30 to your computer and use it in GitHub Desktop.
api.nuget.org connection tester

This app will try to connect to api.nuget.org with HttpClient, expliciting passing each value of the SslProtocols enumeration.

Note that as the docs say, SslProtocol.None actually means "use the operating system defaults". It does not mean "do not use encryption".

If this app shows that Tls12 has failed, try the recommendations from this blog post: https://devblogs.microsoft.com/nuget/deprecating-tls-1-0-and-1-1-on-nuget-org/

If this app shows that Tls12 worked, but None failed, the most common reason is that Windows TLS 1.3 is enabled, however is incompatible with api.nuget.org's CDN provider's implementation. You should disable TLS 1.3 so that Windows no longer attempts to use TLS 1.3 when talking to nuget.org. See dotnet/runtime#40679

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<RootNamespace>tls_test</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Net.Http" Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' " />
</ItemGroup>
</Project>
using System;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks;
namespace tls_test
{
class Program
{
static async Task Main(string[] args)
{
var sslProtocols = (SslProtocols[])Enum.GetValues(typeof(SslProtocols));
foreach (var protocol in sslProtocols)
{
var successful = await TestProtocolAsync(protocol);
var status = successful ? "worked" : "failed";
Console.WriteLine(protocol + " " + status);
}
}
private static async Task<bool> TestProtocolAsync(SslProtocols protocol)
{
try
{
var httpMessageHandler = new HttpClientHandler()
{
SslProtocols = protocol
};
var client = new HttpClient(httpMessageHandler);
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.nuget.org/v3/index.json");
var response = await client.SendAsync(request);
return true;
}
catch
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment