alexalok/dotAPNS

A security error occurred in .NET Framework 4.7.2 when trying to connect using Token

yannisalexiou opened this issue · 7 comments

Below is my case, but I can't figure out what is the cause of my issue. I saw other related issues (like #44) without any luck.
Do you have any thought? Can I try anything else?

Expected Behavior

To send a notification using Token-based connection.

Actual Behavior

Update:
I found that the first time when i trigger this function I get the below error. But if I try second time I take a success response. What is the reason for this not firing success everytime?

The line var response = await apns.SendAsync(push); throws HttpRequestException ex with the error
{"Error 12175 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'A security error occurred'."}

public async Task<string> SendPushAsync(string pushToken)
        {
            ApnsJwtOptions options = new ApnsJwtOptions()
            {
                BundleId = "MyBundleID",
                CertFilePath = "PathTo",
                KeyId = "CertKey",
                TeamId = "TeamID"
            };

            ApnsClient apns = ApnsClient.CreateUsingJwt(new HttpClient(new WinHttpHandler()), options);
            var push = new ApplePush(ApplePushType.Alert).AddToken(pushToken);

            try
            {
                var response = await apns.SendAsync(push);
                if (response.IsSuccessful)
                {
                    Console.WriteLine("An alert push has been successfully sent!");
                    return "DONE";
                }
                else
                {
                    switch (response.Reason)
                    {
                        case ApnsResponseReason.BadCertificateEnvironment:
                            // The client certificate is for the wrong environment.
                            // TODO: retry on another environment
                            break;
                        // TODO: process other reasons we might be interested in
                        default:
                            throw new ArgumentOutOfRangeException(nameof(response.Reason), response.Reason, null);
                    }
                    Console.WriteLine("Failed to send a push, APNs reported an error: " + response.ReasonString);
                    return response.ReasonString;
                }
            }
            catch (TaskCanceledException)
            {
                Console.WriteLine("Failed to send a push: HTTP request timed out.");
                return "Failed to send a push: HTTP request timed out.";
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine("Failed to send a push. HTTP request failed: " + ex);
                return "Failed to send a push. HTTP request failed: " + ex;
            }
        }

Specifications

  • Version: dotAPNS v 3.4.0
  • Platform: .NET Framework 4.7.2, Windows 10 run in IIS Express and local IIS
  • Subsystem: Visual Studio Code 2019

Hi, I too have this problem and I can't send push notifications, not even on subsequent attempts; I always get a generic "security error" when catching HttpRequestException type exceptions.
Same source code as yannisalexiou.

Is it a bug? Am I doing something wrong?

Version: dotAPNS v 3.4.0
Platform: .NET Framework 4.6, Windows 10
Subsystem: Visual Studio Professional 2019

@stemig there seems to be the underlying http stack issue. I'm investigating that but am not sure if I'd be able to identify the root cause.

on localhost it works perfect, but after host on server it gives error
"ClassName": "System.Net.Http.WinHttpException",
"Message": "Error 12175 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'A security error occurred'.",

Probably, after fast restarting project library can recreate new token faster than "every 20 minutes". Previous token was lost but stay valid. Ignore this error first 20 minutes after start, or save current token on disk and restore after start.

"Similarly, APNs reports an error if you recreate your tokens more than once every 20 minutes."

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Shahaji-Mograsys you need upgrade system to win2016 and later

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.