如何在C#中使用APNs身份验证密钥(.p8文件)? [英] How to use APNs Auth Key (.p8 file) in C#?
问题描述
我正在尝试使用基于令牌的身份验证将推送通知发送到iOS设备.
I'm trying to send push notifications to iOS devices, using token-based authentication.
根据需要,我在Apple的Dev Portal中生成了APNs身份验证密钥,并下载了该文件(扩展名为p8的文件).
As required, I generated an APNs Auth Key in Apple's Dev Portal, and downloaded it (it's a file with p8 extension).
要从C#服务器发送推送通知,我需要以某种方式使用此p8文件对我的JWT令牌进行签名.我该怎么办?
To send push notifications from my C# server, I need to somehow use this p8 file to sign my JWT tokens. How do I do that?
我尝试将文件加载到X509Certificate2,但是X509Certificate2似乎不接受p8文件,因此我尝试将文件转换为pfx/p12,但找不到实际可行的方法.
I tried to load the file to X509Certificate2, but X509Certificate2 doesn't seem to accept p8 files, so then I tried to convert the file to pfx/p12, but couldn't find a way to do that that actually works.
推荐答案
我找到了一种使用
现在创建并签名令牌(使用 jose-jwt ): And now creating and signing the token (using jose-jwt): 这篇关于如何在C#中使用APNs身份验证密钥(.p8文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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);
}