Last active
April 9, 2018 11:01
-
-
Save deepumi/49a4cc98d0908674049a64513e54156d to your computer and use it in GitHub Desktop.
Stripe TLS 1.2 integration. Sample code to ensure TLS 1.2 support in OS, library and framework level using C# HttpClient
This file contains 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
//No third party library required for this code. | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
internal static class Stripe | |
{ | |
internal static async Task<string> DoesTLS12Support() | |
{ | |
const string apiEndPoint = "https:api-tls12.stripe.com/v1/charges"; | |
string result = "TLS 1.2 supported, no action required."; | |
try | |
{ | |
var handler = new HttpClientHandler | |
{ | |
Credentials = new NetworkCredential { UserName = "REPLACE_WITH_YOUR_SECRET_KEY" }, | |
PreAuthenticate = true | |
}; | |
using (var client = new HttpClient(handler)) | |
{ | |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; | |
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, apiEndPoint)).ConfigureAwait(false); | |
await response.Content.ReadAsStringAsync().ConfigureAwait(false); | |
} | |
} | |
catch (System.Exception) | |
{ | |
result = "TLS 1.2 is not supported. You will need to upgrade your integration."; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment