C# 微信接口认证

public void valid()
{
string echostr = Request.QueryString["echostr"];
if (!string.IsNullOrEmpty(echostr))
{
if (checkSignature())
{
Response.Write(echostr);
Response.End();
}
}
else
{
return;
}
}

public bool checkSignature()
{
var signature = Request.QueryString["signature"].ToString();
var timestamp = Request.QueryString["timestamp"].ToString();
var nonce = Request.QueryString["nonce"].ToString();
var token = "TMFHTongwx";
string[] ArrTmp = { token, timestamp, nonce };
Array.Sort(ArrTmp, StringComparer.Ordinal); //字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, FormsAuthPasswordFormat.SHA1.ToString());
tmpStr = tmpStr.ToLower();
if (tmpStr.Equals(signature.ToLower()))
{
return true;
}
else
{
return false;
}
}

原文地址:https://www.cnblogs.com/codeloves/p/3361436.html