/// <summary>
/// Post 的摘要说明
/// </summary>
public class Post : IHttpHandler
{
WXCore m_wxCore = new WXCore();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/xml;charset=utf-8";
//context.Response.Write("Hello World"); 打印出这一句,会造成配置不成功
if (context.Request.HttpMethod.ToLower() == "post")
{
}
else
{
m_wxCore.Auth();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
public void Auth()
{
string echoStr = HttpContext.Current.Request.QueryString["echostr"];
if (CheckSignature()) //校验签名是否正确
{
if (!string.IsNullOrEmpty(echoStr))
{
HttpContext.Current.Response.Write(echoStr); //返回原值表示校验成功
HttpContext.Current.Response.End();
}
}
}
/// <summary>
/// 验证微信签名
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
/// </summary>
/// <returns></returns>
private bool CheckSignature()
{
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
//加密/校验流程:
//1. 将token、timestamp、nonce三个参数进行字典序排序
string[] ArrTmp = { ConfigHelper.Token, timestamp, nonce };
Array.Sort(ArrTmp);//字典排序
//2.将三个参数字符串拼接成一个字符串进行sha1加密
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
//3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}