如何在 C# 中实现基于苹果令牌的推送通知(使用 p8 文件)? [英] How to implement apple token based push notifications (using p8 file) in C#?

查看:23
本文介绍了如何在 C# 中实现基于苹果令牌的推送通知(使用 p8 文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有某种基于聊天功能的应用程序,我想添加推送通知支持以接收新消息.我想要做的是使用来自 Apple 的新的基于令牌的身份验证(.p8 文件),但我找不到关于服务器部分的太多信息.

For an app with some kind of chat based features I want to add push notification support for receiving new messages. What I want to do is use the new token based authentication (.p8 file) from Apple, but I can't find much info about the server part.

我看到了以下帖子:如何在 C# 中使用 APNs Auth Key(.p8 文件)?

但是答案并不令人满意,因为没有太多关于如何操作的细节:

However the answer was not satisfying as there was not much detail about how to:

  • 与 APNs 建立连接
  • 使用 p8 文件(除了某种编码)
  • 将数据发送到 Apple 推送通知服务

推荐答案

您目前无法在原始 .NET Framework 上真正做到这一点.新的基于 JWT 的 APNS 服务器仅使用 HTTP/2,.NET Framework 尚不支持.

You can't really do this on raw .NET Framework at the moment. The new JWT-based APNS server uses HTTP/2 only, which .NET Framework does not yet support.

.NET Core 的 System.Net.Http 版本确实如此,前提是您满足以下先决条件:

.NET Core's version of System.Net.Http, however, does, provided you meet the following prerequisites:

  • 在 Windows 上,您必须运行 Windows 10 周年纪念版 (v1607) 或更高版本,或者 Windows Server 2016 的等效版本(我认为).
  • 在 Linux 上,您必须拥有支持 HTTP/2 的 libcurl 版本.
  • 在 macOS 上,您必须编译支持 HTTP/2 的 libcurl,然后使用 DYLD_INSERT_LIBRARIES 环境变量来加载您的 libcurl 自定义构建.
  • On Windows, you must be running Windows 10 Anniversary Edition (v1607) or higher, or the equivalent build of Windows Server 2016 (I think).
  • On Linux, you must have a version of libcurl that supports HTTP/2.
  • On macOS, you have to compile libcurl with support for HTTP/2, then use the DYLD_INSERT_LIBRARIES environment variable in order to load your custom build of libcurl.

如果您真的需要,您应该能够在 .NET Framework 中使用 .NET Core 版本的 System.Net.Http.

You should be able to use .NET Core's version of System.Net.Http in the .NET Framework if you really want.

我不知道在 Mono、Xamarin 或 UWP 上会发生什么.

I have no idea what happens on Mono, Xamarin or UWP.

然后你必须做三件事:

  1. 解析您获得的私钥.这是当前的 ECDSA 密钥,您可以将其加载到 System.Security.Cryptography.ECDsa 对象中.

  • 在 Windows 上,您可以使用 CNG API.在解析密钥文件的 base64 编码的 DER 部分后,您可以使用 new ECDsaCng(CngKey.Import(data, CngKeyBlobFormat.Pkcs8PrivateBlob)) 创建一个密钥.
  • 在 macOS 或 Linux 上不支持 API,您必须自己解析 DER 结构,或使用第三方库.
    1. 创建一个 JSON 网络令牌/不记名令牌.如果您使用 NuGet 的 System.IdentityModel.Tokens.Jwt 包,这相当简单.您将需要 Apple 提供的密钥 ID 和团队 ID.
    1. Create a JSON Web Token / Bearer Token. If you use the System.IdentityModel.Tokens.Jwt package from NuGet, this is fairly simple. You will need the Key ID and Team ID from Apple.

    public static string CreateToken(ECDsa key, string keyID, string teamID)
    {
        var securityKey = new ECDsaSecurityKey(key) { KeyId = keyID };
        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.EcdsaSha256);
    
        var descriptor = new SecurityTokenDescriptor
        {
              IssuedAt = DateTime.Now,
              Issuer = teamID,
              SigningCredentials = credentials
        };
    
        var handler = new JwtSecurityTokenHandler();
        var encodedToken = handler.CreateEncodedJwt(descriptor);
        return encodedToken;
    }

    1. 发送 HTTP/2 请求.这很正常,但您需要做两件额外的事情:
    2. yourRequestMessage.Version 设置为 new Version(2, 0) 以便使用 HTTP/2 发出请求.
    3. yourRequestMessage.Headers.Authorization 设置为 new AuthenticationHeaderValue("bearer", token) 以便为您的请求提供不记名身份验证令牌/JWT.莉>
    1. Send an HTTP/2 request. This is as normal, but you need to do two extra things:
    2. Set yourRequestMessage.Version to new Version(2, 0) in order to make the request using HTTP/2.
    3. Set yourRequestMessage.Headers.Authorization to new AuthenticationHeaderValue("bearer", token) in order to provide the bearer authentication token / JWT with your request.

    然后只需将您的 JSON 放入 HTTP 请求并将其 POST 到正确的 URL.

    Then just put your JSON into the HTTP request and POST it to the correct URL.

    这篇关于如何在 C# 中实现基于苹果令牌的推送通知(使用 p8 文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆