PushSharp APNS生产环境:提供给包的凭据没有被识别(虽然开发环境可以工作)

29

我的应用程序刚刚在 App Store 上准备上架,但是我所有已经从 App Store 安装了该应用程序的生产设备都无法接收推送通知。当我尝试向生产设备发送推送通知时,我得到了以下错误:

"The credentials supplied to the package were not recognized" 
(System.ComponentModel.Win32Exception)

这个异常在一个无限循环中被内部抛出并捕获:

enter image description here

它在ApplePushChannel.cs文件的第539行抛出:

    try
{
    stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, 
        System.Security.Authentication.SslProtocols.Ssl3, false);
    //stream.AuthenticateAsClient(this.appleSettings.Host);
}
catch (System.Security.Authentication.AuthenticationException ex)
{
    throw new ConnectionFailureException("SSL Stream Failed to Authenticate as Client", ex);
}

这是在Visual Studio输出中显示的应用程序输出:

...
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
...(it keeps getting thrown until I stop it manually)

这是我尝试过的:

  • 双重检查我正在尝试使用的设备ID是否已注册生产设备令牌。
  • 吊销并重新生成APNS生产证书,将其与私钥一起导出到新的.p12文件中,并尝试使用新证书再次发送推送通知。(我在处理开发推送通知时遇到了相同的问题,这解决了我的问题)。
  • 将SSL协议从Ssl3更改为Tls。(几天前有一个协议版本问题,而它暂时解决了一个问题。不应该需要这样做,但我收到的错误与之前遇到的错误相同,而这种方法可以解决)。
  • 检查我是实际上正在尝试使用生产证书连接到生产服务器,而不是开发服务器/证书。
  • 检查我能否直接访问APNS服务器(我的ASP.NET应用程序位于我的Mac上的Parallels VM Windows 8.1中,以下是来自我的Mac的输出,只是为了避免混淆:)

(终端输出) 编辑:我正在ping沙盒服务器,我已经ping了生产服务器,我确认我也可以连接到它,所以这不是问题所在。

can$ sudo nmap -p 2195 gateway.sandbox.push.apple.com
Starting Nmap 6.40-2 ( http://nmap.org ) at 2014-04-28 00:06 EEST
Nmap scan report for gateway.sandbox.push.apple.com (17.149.34.189)
Host is up (0.49s latency).
Other addresses for gateway.sandbox.push.apple.com (not scanned): 17.149.34.187 17.149.34.188
PORT     STATE SERVICE
2195/tcp open  unknown

为什么PushSharp无法与APNS服务器协商?

6个回答

81

我找到了问题。我撤销并重新生成证书,这次只导出了私钥(不包括证书)。在钥匙串访问中,我将其导出为.p12文件,并使用了新文件,结果可以正常工作。由于某种原因,PushSharp无法与同时存在证书和私钥的.p12文件兼容。


3
谢谢,我刚刚重新做了证书,现在可用了!虽然我认为这不是PushSharp的问题,我想是在制作这些证书的非常复杂的过程中出了点问题。所以重新开始似乎纠正了这个问题。 - rich97
1
我觉得我应该分享一下我的经验,也许有一天会帮到某个人。 - rich97
8
这个。这个。所有的文档都告诉你要导出两个项目,然后却失败了。在这上面浪费了无数个小时。谢谢@CanPoyrazoğlu。 - Dean Thomas
1
在尝试了您的建议(仅导出私钥而不是证书)之前,我也浪费了一个小时在这个问题上。更改后问题得到了解决! - Bishbulb
4
由于某些原因,从Mac上的PHP导出这两个项目可以正常工作,但在C#服务器上失败了。通过仅使用私钥,已使其正常工作。 - Alex Terreaux
显示剩余14条评论

3
"提供给程序包的凭据未被识别"异常通常表示运行代码的用户没有足够的权限。
如果您正在从Azure网站或WebJob发送推送通知,请勿从文件或base64编码的字符串加载APNS证书。请前往Azure门户并将证书添加到网站中。请注意拇指印。

certificate in Azure Portal

接下来添加WEBSITE_LOAD_CERTIFICATES设置,并将其设置为*(星号)。

现在可以从C#代码中使用APNS证书:

string thumbprint = "YOUR THUMBPRINT";
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certificate = store.Certificates.Find(
    X509FindType.FindByThumbprint, thumbprint, validOnly: false)
    .Cast<X509Certificate2>().SingleOrDefault();
var apnsConfig = new ApnsConfiguration(
    ApnsConfiguration.ApnsServerEnvironment.Production, certificate);

参考资料


这个答案对我有帮助,但是需要做一些更改。 现在在新的Azure门户中,您可以在App Service / SSL设置/私有证书(.pfx)/上传证书中添加证书。 有趣的是,您不需要更改OP使用的代码(stream.AuthenticateAsClient),因此您无需在缩略图或此答案中的代码方面烦恼。在上传证书后,旧代码将立即开始工作,当然还需要WEBSITE_LOAD_CERTIFICATES。遗憾的是,您需要付费计划(至少基本计划)才能上传任何证书。 - Andrzej Martyna

1

对我来说,所有的答案都没有用。最终我做的是将证书和私钥导入到Windows证书存储中,然后导出为.pfx


1
当使用Windows证书存储(在我看来是在生产服务器上管理证书的最简单方法)时,请确保在私钥上设置正确的权限。

我在生产环境中收到了这个错误(在开发环境中它是正常的)。重新创建证书没有起作用,但这里给出的答案有效。 - Jeroen Visscher

1
我一遍又一遍地测试了它。
p12文件转换为pem格式,它将与IIS受限用户一起工作,也可能与Azure一起使用...。

0

我曾经遇到同样的异常情况,我的解决方法是为我的IOS推送服务证书添加权限。

在mmc中右键点击证书 -> 所有任务 -> 管理私钥... 我添加了NETWORK SERVICE,因为我的Web应用程序的IIS应用程序池使用了该帐户。

更多详细信息请参见:http://blog.falafel.com/apple-push-notifications-certificates-and-iis/


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