I am relatively new to much of this and attempting to send notifications to APNS API using asp.net 4.5 in a windows console program (eventually will be a Windows Service).
But my code below in the console program is returning an error exception in the "response.ErrorException" with the following:
System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
I've spent many hours trying to figure out what is wrong without any success. Unfortunately I am working by myself so any advice or help is appreciated!
Here is what I have done so far:
- Downloaded the APNS certificate from Apple and downloaded on my local Windows 7 machine.
- Used the instructions posted here to install the certificate on my local machine.
- The certificate is installed on the Local User Store as mentioned to using Postman.
- Tested the HTTP POST with Postman and got a successful response (200 OK)
Using Postman code generation, and adding a little of my own code taken from other posts, I inserted the following code into my console application:
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
var client = new RestClient("https://api.development.push.apple.com/3/device/cMQCjCHfLAM:APA91bF2D............IIZJ85henm5zPgY5");
client.ClientCertificates = new X509CertificateCollection();
client.ClientCertificates.Add(new X509Certificate("apn_development.p12", "testpassword");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("apns-topic", "com.test.test");
request.AddHeader("host", "api.development.push.apple.com");
request.AddHeader("content-type", "application/json");
request.AddHeader("apns-priority", "10");
request.AddHeader("apns-expiration", "0");
request.AddParameter("application/json", "{\r\n \"aps\": {\r\n \"alert\": {\r\n \"title\": \"testtest\",\r\n \"body\": \"This is a test.\"\r\n },\r\n \"badge\": 0\r\n }\r\n}\r\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.ErrorException != null)
{
log.Error(response.ErrorMessage.FirstOrDefault());
Debug.WriteLine(response.ErrorMessage.FirstOrDefault());
}
}
catch (Exception ex)
{
log.Error(ex);
Debug.WriteLine(ex.InnerException);
}
I also added System.net trace logging and got the following though I don't know what they mean. Here is a small snippet of the trace log:
System.Net.Sockets Verbose: 0 : [7912] 00000395 : 63 6F 6E 64 69 74 69 6F-6E 73 20 6F 66 20 75 73 : conditions of us
System.Net.Sockets Verbose: 0 : [7912] 000003A5 : 65 20 61 6E 64 2F 6F 72-20 63 65 72 74 69 66 69 : e and/or certifi
System.Net.Sockets Verbose: 0 : [7912] 000003B5 : 63 61 74 69 6F 6E 20 70-72 61 63 74 69 63 65 20 : cation practice
System.Net.Sockets Verbose: 0 : [7912] 000003C5 : 73 74 61 74 65 6D 65 6E-74 73 2E 30 39 06 08 2B : statements.09..+
System.Net.Sockets Verbose: 0 : [7912] 000003D5 : 06 01 05 05 07 02 01 16-2D 68 74 74 70 3A 2F 2F : ........-http://
System.Net.Sockets Verbose: 0 : [7912] 000003E5 : 77 77 77 2E 61 70 70 6C-65 2E 63 6F 6D 2F 63 65 : www.apple.com/ce
System.Net.Sockets Verbose: 0 : [7912] 000003F5 : 72 74 69 66 69 63 61 74-65 61 75 74 68 6F 72 69 : rtificateauthori
System.Net.Sockets Verbose: 0 : [7912] Exiting Socket#44665200::Receive() -> Int32#2576
System.Net Information: 0 : [7912] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 52f5bc8:53198b8, targetName = api.development.push.apple.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [7912] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=ContinueNeeded).
System.Net.Sockets Verbose: 0 : [7912] Socket#44665200::Receive()
System.Net.Sockets Verbose: 0 : [7912] Data from Socket#44665200::Receive
System.Net.Sockets Verbose: 0 : [7912] 00000000 : 15 03 03 00 02 : .....
System.Net.Sockets Verbose: 0 : [7912] Exiting Socket#44665200::Receive() -> Int32#5
System.Net.Sockets Verbose: 0 : [7912] Socket#44665200::Receive()
System.Net.Sockets Verbose: 0 : [7912] Data from Socket#44665200::Receive
System.Net.Sockets Verbose: 0 : [7912] 00000005 : 02 28 : .(
System.Net.Sockets Verbose: 0 : [7912] Exiting Socket#44665200::Receive() -> Int32#2
System.Net Information: 0 : [7912] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 52f5bc8:53198b8, targetName = api.development.push.apple.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [7912] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=IllegalMessage).
System.Net.Sockets Verbose: 0 : [7912] Socket#44665200::Dispose()
System.Net Error: 0 : [7912] Exception in HttpWebRequest#22369618:: - The request was aborted: Could not create SSL/TLS secure channel..
System.Net Error: 0 : [7912] Exception in HttpWebRequest#22369618::EndGetRequestStream - The request was aborted: Could not create SSL/TLS secure channel..