4

firebase 通知代码

WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new{collapse_key = "unassigned", to = deviceToken,data = new
  {body = message,title = title,sound = "default"}
};

在移动设备上传递通知的消息

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationId));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;

错误发生在下面的代码

using (Stream dataStream = tRequest.GetRequestStream())
{ 
  dataStream.Write(byteArray, 0, byteArray.Length);
 using (WebResponse tResponse = tRequest.GetResponse())
  { 
    using (Stream dataStreamResponse = tResponse.GetResponseStream())
    { 

   //code 1
    }     
  }   
}  
4

4 回答 4

12

标题中的异常表示您正在使用 TLS 加密连接到端点,并且该端点公开的证书不受您的信任。这意味着它没有使用您在 CA(证书颁发机构)存储中拥有的证书进行签名。就像一个自签名证书。

如果证书是自签名的,您可以将其添加到您的 CA 存储中。如果没有,您可以尝试使用浏览器导航端点,并查找端点提供的证书副本,以手动信任它。(请注意,如果端点已被入侵,则执行此操作是在手动信任其证书。)

您还可以通过添加始终返回有效的自定义证书验证处理程序来避免此检查!(真的)。但是,请注意,这样做会使您面临中间人攻击,因为您将失去检查端点真实性的能力

ServicePointManager
    .ServerCertificateValidationCallback += 
    (sender, cert, chain, sslPolicyErrors) => true;
于 2017-01-07T11:02:30.020 回答
1

I don't why using this line of code stated above

ServicePointManager
    .ServerCertificateValidationCallback += 
    (sender, cert, chain, sslPolicyErrors) => true;

i could not made it work

So but using it this way worked properly:

internal static bool ValidateServerCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
}
ServicePointManager.ServerCertificateValidationCallback  = ValidateServerCertificate;
于 2018-02-28T21:31:52.630 回答
0
  1. 调用网站和 API 站点应具有相同的 SSL 证书,我使用“IIS Express 开发证书”</p>

  2. 将您的证书导出到本地文件 导出证书

  3. 将您的证书导入“ConsoleCertificate”中的“Trusted Root Certification Authorities”文件夹</p>

将其导入信任根

于 2019-12-02T20:53:09.430 回答
-1

这对我有用。

ServicePointManager
  .ServerCertificateValidationCallback += 
  (sender, cert, chain, sslPolicyErrors) => true;

我在有无的情况下对其进行了多次测试。绝对解决了这个问题。

于 2018-09-24T22:17:09.487 回答