如何在C#中使用APNs认证密钥(.p8文件)?

14

我正在尝试使用基于令牌的身份验证向iOS设备发送推送通知。

按要求,我在苹果开发者门户网站上生成了一个APNs Auth Key,并下载了它(它是一个.p8文件)。

为了从我的C#服务器发送推送通知,我需要以某种方式使用此p8文件来签署我的JWT令牌。我该怎么做呢?

我尝试将文件加载到X509Certificate2中,但X509Certificate2似乎不接受p8文件,因此我尝试将文件转换为pfx/p12格式,但找不到可行的方法。

3个回答

9
我发现了一种方法,使用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);
    }
}

现在使用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);
}

3
你能分享一下你用于上传令牌和有效负载数据的代码吗?我已经尝试了几个小时,但是一直得到“连接关闭”的错误提示。 - Brad C
1
你好,我该如何使用这个令牌调用苹果的网络服务? - matti157
请问您能否添加更多细节说明如何将帖子发送到APN服务?我有一段代码,与下面的线程中的代码非常相似,并且我得到了相同的错误。任何帮助都将不胜感激。 https://stackoverflow.com/questions/53378912/net-core-2-1-apple-push-notifications - civilator

1
我希望这将是一个解决方案;
private static string GetToken(string fileName)
    {
        var fileContent = File.ReadAllText(fileName).Replace("-----BEGIN PRIVATE KEY-----", "").Replace
            ("-----END PRIVATE KEY-----", "").Replace("\r", "");
        
        var signatureAlgorithm = GetEllipticCurveAlgorithm(fileContent);

        ECDsaSecurityKey eCDsaSecurityKey = new ECDsaSecurityKey(signatureAlgorithm)
        {
            KeyId = "S********2"
        };

        var handler = new JwtSecurityTokenHandler();   
        JwtSecurityToken token = handler.CreateJwtSecurityToken(
            issuer: "********-****-****-****-************",
            audience: "appstoreconnect-v1",
            expires: DateTime.UtcNow.AddMinutes(5), 
            issuedAt: DateTime.UtcNow,
            notBefore: DateTime.UtcNow,
            signingCredentials: new SigningCredentials(eCDsaSecurityKey, SecurityAlgorithms.EcdsaSha256));

        return token.RawData;

    }
    
    private static ECDsa GetEllipticCurveAlgorithm(string privateKey)
    {
        var keyParams = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));

        var normalizedEcPoint = keyParams.Parameters.G.Multiply(keyParams.D).Normalize();

        return ECDsa.Create(new ECParameters
        {
            Curve = ECCurve.CreateFromValue(keyParams.PublicKeyParamSet.Id),
            D = keyParams.D.ToByteArrayUnsigned(),
            Q =
            {
                X = normalizedEcPoint.XCoord.GetEncoded(),
                Y = normalizedEcPoint.YCoord.GetEncoded()
            }
        });
    }

你能简要地解释一下为什么这会是一个解决方案吗? - XouDo
这对我连接到应用商店服务器API很有效!谢谢 - Dragouf

0

创建密钥的一种方法,无需使用BouncyCastle:

    var privateKeyString = (await File.ReadAllTextAsync("<PATH_TO_KEY_FILE>"))
        .Replace("-----BEGIN PRIVATE KEY-----", "")
        .Replace("-----END PRIVATE KEY-----", "")
        .Replace("\n", "");
    using var algorithm = ECDsa.Create();
    algorithm.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKeyText), out var _);
    var securityKey = new ECDsaSecurityKey(algorithm) { KeyId = "<KEY_ID>" };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接