Asp.Net Core 微信接口配置


先看代码:

private bool CheckSignature(string token, string signature, string timestamp, string nonce)
{
    string[] ArrTmp = { token, timestamp, nonce };
    Array.Sort(ArrTmp);
    string tmpStr = string.Join("", ArrTmp);
    byte[] tmpByte = Encoding.UTF8.GetBytes(tmpStr);
    tmpByte = SHA1.Create().ComputeHash(tmpByte);
    tmpStr = BitConverter.ToString(tmpByte);
    tmpStr = tmpStr.Replace("-", tmpStr).ToLower();
    //tmpStr= BitConverter.ToString(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(tmpStr))).Replace("-", "").ToLower();
    
    if (tmpStr == signature)
    {
        return true;
    }
    else
    {
        return false;
    }
}

这里有两点需要注意地方:

  • 编码字符串时要使用Encoding.UTF8
  • 编码转换成字符串时用BitConverter.ToString
Published 27 September 2016 by HelloDBA