How to use APNs Auth Key (.p8 file) in C #?

I am trying to send push notifications to iOS devices using token based authentication.

As required, I generated APNs Auth Key in the Apple Dev Portal and uploaded it (it is a p8 file).

In order to send push notifications from my C # server I need to somehow use this p8 file to sign my JWT tokens. How to do it?

I tried uploading the file to X509Certificate2 but X509Certificate2 doesn't seem to accept p8 files, so I tried converting the file to pfx / p12 but couldn't find a way to do it, which actually works.

+11


source to share


1 answer


I found a way to do this using BouncyCastle :

private static CngKey GetPrivateKey()
{
    using (var reader = File.OpenText("path/to/apns/auth/key/file.p8"))
    {
        var ecPrivateKeyParameters = (ECPrivateKeyParameters)new PemReader(reader).ReadObject();
        var x = ecPrivateKeyParameters.Parameters.G.AffineXCoord.GetEncoded();
        var y = ecPrivateKeyParameters.Parameters.G.AffineYCoord.GetEncoded();
        var d = ecPrivateKeyParameters.D.ToByteArrayUnsigned();
        return EccKey.New(x, y, d);
    }
}

      



And now we create and sign the token (using jose-jwt ):

private static string GetProviderToken()
{
    var epochNow = (int) DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
    var payload = new Dictionary<string, object>()
    {
        {"iss", "your team id"},
        {"iat", epochNow}
    };
    var extraHeaders = new Dictionary<string, object>()
    {
        {"kid", "your key id"}
    };
    var privateKey = GetPrivateKey();
    return JWT.Encode(payload, privateKey, JwsAlgorithm.ES256, extraHeaders);
}

      

+5


source







All Articles