C# PushSharp&x27;s通知Apple在服务器上生成临时RSA文件
我的WCF推送通知服务有问题。 每次它向苹果发送推送通知时,都会在“C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys”文件夹中生成一个新的RSA。 我认为造成这种情况的问题可能与Apple p12证书有关 为什么会这样?有没有办法防止这些RSA的生成 我正在使用PushSharp库,我的代码如下所示:C# PushSharp&x27;s通知Apple在服务器上生成临时RSA文件,c#,apple-push-notifications,rsa,windows-server-2008-r2,pushsharp,C#,Apple Push Notifications,Rsa,Windows Server 2008 R2,Pushsharp,我的WCF推送通知服务有问题。 每次它向苹果发送推送通知时,都会在“C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys”文件夹中生成一个新的RSA。 我认为造成这种情况的问题可能与Apple p12证书有关 为什么会这样?有没有办法防止这些RSA的生成 我正在使用PushSharp库,我的代码如下所示: pushBroker = new PushBroker(); pushBroker.RegisterAppleService( new Pus
pushBroker = new PushBroker();
pushBroker.RegisterAppleService(
new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, p12Pass, false)
, null);
我也有同样的问题,我找到了解决办法 为了防止每次发送新推送通知时生成密钥, 代码应该是这样的。它对我来说非常适合
var cert = X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable;
var certificate1 = new X509Certificate2(appleCert, p12CerPass,cert);
_broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(certificate1, false));
临时文件是Windows将其从
appleCert
PFX blob加载的RSA私钥放入其中的位置
如果该文件出现,然后消失(因为您将其称为“临时”文件),则一切正常。如果它出现并永久保留,则PushSharp将使用PersistKeySet加载证书,这表示不应删除私钥
我不知道它是否是ApplePushChannelSettings的真正来源,但它会的。查看该源代码,他们似乎在使用PersistKeySet
,使文件永久化
您有两种实用方法:
1) 将证书作为一次性操作加载到X509Store中(在加载时使用PersistKeySet,因为需要存储持久性)。该文件将成为永久文件,但会被永久使用,因此很酷
2) 您自己从PFX加载证书,并(理想情况下)在完成后处理证书。我不知道您是否有任何类型的关闭时通知可以或多或少可靠地使用,但是调用cert.Dispose()
(或者,在较旧的框架版本上,cert.Close()
)比等待GC和终结器为您进行清理更可靠
如果您确实有一个良好的关机事件:
// Note that I'm not specifying any X509KeyStorageFlags here.
// You don't want it PersistKeySet (that's the whole point) and you don't
// need it to be exportable (unless you get exceptions without Exportable, in which case
// PushSharp is doing something naughty).
private X509Certificate _cert = new X509Certificate2(appleCert, p12CerPass);
...
_broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(_cert, false));
...
// In some OnShutdown type location
_cert.Dispose();
如果没有:
// Still don't specify any load flags. Especially not PersistKeySet.
var cert = new X509Certificate2(appleCert, p12CerPass);
_broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(cert, false));
不管怎样,每次进程异常终止时,您仍然会得到一个永久泄漏的文件,因为文件删除代码没有机会启动
存在一种完全避免创建临时文件的解决方案,即使用添加到.NET Core 2.0(当前处于预览中)的EphemeralKeySet标志。它在.NET Framework中尚不可用
// Keep the private key in memory, never let it touch the hard drive.
var cert = new X509Certificate2(appleCert, p12CerPass, X509KeyStorageFlags.EphemeralKeySet);
_broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(cert, false));
但是我不知道PushSharp是否可用于.NET Core。通过指定
PersistKeySet
每次运行构造函数时,您都将文件永久保存在磁盘上。除非需要,否则不应将设置为可导出。