如何验证第三方服务器.net核心上的GKLocalPlayer

时间:2018-02-23 12:12:47

标签: c# asp.net-core game-center

我有以下代码用于验证GKLocalPlayer:

var cert = await GetCertificate(gameCenter.PublicKeyURL);
if (cert.Verify())
{
    var rsa = cert.GetRSAPublicKey();
    if (rsa != null)
    {
        var sha256 = new SHA256Managed();
        var sig = ConcatSignature(gameCenter.PlayerID, gameCenter.BundleID, gameCenter.TimeStamp, gameCenter.Salt);

        var hash = sha256.ComputeHash(sig);
        if (rsa.VerifyHash(hash, Convert.FromBase64String(gameCenter.Signature), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1))
        {
            return true;
        }
        return false;
    }
}

private async Task<X509Certificate2> GetCertificate(string url)
{
    var client = new HttpClient();
    var response = await client.GetAsync(url);
    var rawData = await response.Content.ReadAsByteArrayAsync();
    return new X509Certificate2(rawData);
}

private byte[] ConcatSignature(string playerId, string bundleId, string timestamp, string salt)
{
    var b = Convert.FromBase64String(salt);

    var data = new List<byte>();
    data.AddRange(Encoding.UTF8.GetBytes(playerId));
    data.AddRange(Encoding.UTF8.GetBytes(bundleId));
    data.AddRange(ToBigEndian(Convert.ToUInt64(timestamp)));
    data.AddRange(Convert.FromBase64String(salt));
    return data.ToArray();
}

private static byte[] ToBigEndian(ulong value)
{
    var buffer = new byte[8];
    for (int i = 0; i < 8; i++)
    {
        buffer[7 - i] = unchecked((byte)(value & 0xff));
        value = value >> 8;
    }
    return buffer;
}

但是当我试图验证准确的GameCenter时,这总是返回false。我浏览了所有评论,但是找不到专门为.net Core和GKLocalPlayer验证发布的内容。

1 个答案:

答案 0 :(得分:0)

您的代码对我有用。 请注意,如果您不在设备上对其进行测试,则它将失败(因为数据是时间敏感的)