/// <summary>
/// 固定接入验证TOKEN
/// </summary>
const string Token = "H1Y2D3E4E5P6R7O8";
protected void Page_Load(object sender, EventArgs e)
{
string echostr = Request.QueryString["echostr"];
if (CheckSignature())
{
if (!string.IsNullOrEmpty(echostr))
{
Response.Write(echostr);
Response.End();
}
}
}
#region 微信接口对接验证代码
/// <summary>
/// 验证微信签名
/// </summary>
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
/// <returns></returns>
private bool CheckSignature()
{
string signature = Request.QueryString["signature"];//.ToString();
string timestamp = Request.QueryString["timestamp"];//.ToString();
string nonce = Request.QueryString["nonce"];//.ToString();
string[] ArrTmp = { Token, timestamp, nonce };
Array.Sort(ArrTmp); //字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
#endregion