服务器2008上的HttpTwo客户端(带有JWT的APNS)

时间:2017-08-01 15:18:23

标签: c# windows-server-2008 http2

编辑 - 由于可能的误解: 这与HTTP / 2的服务器端无关 - 它与旧服务器操作系统的客户端HTTP / 2请求有关。另外,我使用python(gobiko.apns)让它工作,所以在我看来它应该是可能的。

编辑2 似乎这个问题与HTTP2没什么关系,而是苹果要求的密码。在pre-win10版本中,SslStream不使用TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384。但是,因为它可以使用python完成,在我看来它应该是可能的。任何帮助将不胜感激。

我们在这里和那里找到了一些代码,以便我们与开发环境中的APNS建立联系。我们正在使用.p8证书并签署令牌作为授权(而不是旧的'界面)。

这适用于我的开发者电脑(win10),但是当我将它转移到服务器2008 R2时,它会发出一个奇怪的警告。这似乎与tls连接的设置有关,但是,我对这个区域并不太熟悉。我真的搜索过,但我能想到的唯一一件事就是服务器2008R2不会支持它,因为密码或其他东西(这对我来说似乎是不合理的)。

从我的电脑上运行的代码(使用nuget HttpTwo和Newtonsoft):

    public static async void Send2(string jwt, string deviceToken)
    {
        var uri = new Uri($"https://{host}:443/3/device/{deviceToken}");

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        ServicePointManager.ServerCertificateValidationCallback = delegate
        {
            Console.WriteLine("ServerCertificateValidationCallback");
            return true;
        };

        string payloadData = Newtonsoft.Json.JsonConvert.SerializeObject(new
        {
            aps = new
            {
                alert = new
                {
                    title = "hi",
                    body = "works"
                }
            }
        });

        //PayloadData always in UTF8 encoding
        byte[] data = System.Text.Encoding.UTF8.GetBytes(payloadData);

        var httpClient = new Http2Client(uri);
        var headers = new NameValueCollection();

        headers.Add("authorization", string.Format("bearer {0}", jwt));
        headers.Add("apns-id", Guid.NewGuid().ToString());
        headers.Add("apns-expiration", "0");
        headers.Add("apns-priority", "10");
        headers.Add("apns-topic", bundleId);

        try
        {
            var responseMessage = await httpClient.Send(uri, HttpMethod.Post, headers, data);
            if (responseMessage.Status == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Send Success");
                return;
            }
            else
            {
                Console.WriteLine("failure {0}", responseMessage.Status);

                return;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ex");
            Console.WriteLine(ex.ToString());
            return;
        }
    }

投掷

System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted

来自服务器2008R2。

我还尝试使用WinHttpHandler,它也适用于我的电脑,但是会抛出

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred

Stacktraces主要是异步的东西,但对于HttpTwo实现,它归结为HttpTwo.Http2Connection.<Connect>,对于WinHttpHandler,归结为System.Net.Http.WinHttpHandler.<StartRequest>

我是否需要添加到服务器以便工作/我们能让它工作吗?

更新 我在我的项目中包含了来自HttpTwo的源文件并对其进行了调试。

发生异常
      await sslStream.AuthenticateAsClientAsync (
                ConnectionSettings.Host, 
                ConnectionSettings.Certificates ?? new X509CertificateCollection (), 
                System.Security.Authentication.SslProtocols.Tls12, 
                false).ConfigureAwait (false);

在我的Win8测试电脑上。现在,当我在我自己的PC上使用只有主机参数的方法重载时,它会抛出相同的异常,我猜是因为tls协议已关闭。

根据这个Github issue,它可能与密码有关。我之前遇到过一些问题,但在我看来,至少WIN8 PC必须能够就足够安全的密码达成一致,对吗?

Schannel抱怨&#34;从远程端点收到致命警报。 TLS协议定义了致命的警报代码是40。&#34;,所以指向那个方向也是afaik。

1 个答案:

答案 0 :(得分:1)

它是密码...... Apple使用的密码不包含在Win10 / Server2016下面的SChannel中。 (见编辑2)

相关问题