/// <summary>
/// 验证微信签名
/// </summary>
/// <returns></returns>
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
private bool CheckSignature()
{
string access_token = "weixin";
string signature = HttpContext.Current.Request.QueryString["signature"].ToString();
string timestamp = HttpContext.Current.Request.QueryString["timestamp"].ToString();
string nonce = HttpContext.Current.Request.QueryString["nonce"].ToString();
string[] ArrTmp = { access_token, timestamp, nonce };
Array.Sort(ArrTmp); //字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
if (tmpStr.ToLower() == signature)
{
return true;
}
else
{
return false;
}
}
这段代码源自:点击打开链接, 表示感谢。
现在没时间整理出 core 版, 晚一点再实现。