28 lines
853 B
C#
28 lines
853 B
C#
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Networking;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Security.Cryptography.X509Certificates;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 信任指定自签名证书
|
|||
|
|
/// </summary>
|
|||
|
|
public class SelfSignedCertHandler : CertificateHandler
|
|||
|
|
{
|
|||
|
|
// 受信任的证书数据(从 StreamingAssets 加载)
|
|||
|
|
private X509Certificate2 _trustedCert;
|
|||
|
|
|
|||
|
|
public SelfSignedCertHandler(byte[] certData)
|
|||
|
|
{
|
|||
|
|
_trustedCert = new X509Certificate2(certData);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected override bool ValidateCertificate(byte[] certificateData)
|
|||
|
|
{
|
|||
|
|
// 方法1:严格匹配整本证书(推荐)
|
|||
|
|
var remoteCert = new X509Certificate2(certificateData);
|
|||
|
|
return remoteCert.Thumbprint == _trustedCert.Thumbprint;
|
|||
|
|
|
|||
|
|
// 方法2:宽松模式 - 只要证书能解析就信任(调试用,危险)
|
|||
|
|
// return true;
|
|||
|
|
}
|
|||
|
|
}
|