public void Valid(WeChatRequest model) { Logger.Info(Json(model)); string respMessage = ""; //获取请求来的 echostr 参数 string echoStr = model.echostr; //通过验证 if (WxHelper.CheckSignature(model)) { respMessage = echoStr; } if (HttpContext.Request.HttpMethod.ToUpper() == "POST") { //从请求的数据流中获取请求信息 using (Stream stream = HttpContext.Request.InputStream) { byte[] postBytes = new byte[stream.Length]; stream.Read(postBytes, 0, (int)stream.Length); string postString = System.Text.Encoding.UTF8.GetString(postBytes); Handle(postString, model); } } if (string.IsNullOrEmpty(respMessage)) { return; } //将随机生成的 echostr 参数 原样输出 Response.Write(respMessage); //截止输出流 Response.End(); }
protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { WeChatRequest model = new WeChatRequest { nonce = filterContext.HttpContext.Request.QueryString["nonce"], signature = filterContext.HttpContext.Request.QueryString["signature"], timestamp = filterContext.HttpContext.Request.QueryString["timestamp"] }; //验证 if (WxHelper.CheckSignature(model)) { base.OnActionExecuting(filterContext); } }