.NET MVC微信公众账号验证
鸣谢:Senparc.Weixin,刘捷
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using CYTD.WebChat.ClassLibrary; using System.Web.Configuration;
namespace CYTDWebChatMain.Controllers { public class HomeController : Controller { public readonly string Token = "jasmine"; //public static readonly string Token = WebConfigurationManager.AppSettings["Token"];//与微信公众账号后台的Token设置保持一致,区分大小写。 //public static readonly string EncodingAESKey = WebConfigurationManager.AppSettings["EncodingAESKey"];//与微信公众账号后台的EncodingAESKey设置保持一致,区分大小写。 //public static readonly string AppId = WebConfigurationManager.AppSettings["AppID"];//与微信公众账号后台的AppId设置保持一致,区分大小写。 /// <summary> /// 微信后台验证地址(使用Get) /// </summary> [HttpGet] [ActionName("Index")] public ActionResult Get(string signature, string timestamp, string nonce, string echostr) { if (CheckSignature.Check(signature, timestamp, nonce, Token)) { return Content(echostr);//返回随机字符串则表示验证通过 } else { return Content("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, Token)); } }
/// <summary> /// 用户发送消息后,微信平台自动Post一个请求到这里,并等待响应XML /// </summary> [HttpPost] [ActionName("Index")] public ActionResult Post(string signature, string timestamp, string nonce, string echostr) { if (!CheckSignature.Check(signature, timestamp, nonce, Token)) { return Content("参数错误!"); }
//var messageHandler = new CustomMessageHandler(Request.InputStream);//接收消息
//messageHandler.Execute();//执行微信处理过程
//return Content(messageHandler.ResponseDocument.ToString());//v0.7- //return new WeixinResult(messageHandler);//v0.8+ with MvcExtension return View(); }
} }










