您的位置:首页 > 编程语言 > ASP

ASP.NET MVC 微信公共平台开发之验证消息的真实性

2015-03-24 07:37 686 查看
ASP.NET MVC 微信公共平台开发

验证消息的真实性

在MVC Controller所在项目中添加过滤器,在过滤器中重写
public override void OnActionExecuting(ActionExecutingContext filterContext)方法

新建数据模型

/// <summary>
/// 日志助手
/// </summary>
private static Common.LogHelper logger = new Common.LogHelper(typeof(HomeController));

[Filters.WeChatRequestValid]
public void Valid(Model.FormatModel.WeChatMsgRequestModel model)
{
if (ModelState.IsValid)
{
try
{
//判断是否是POST请求
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);
}
}
}
catch (Exception ex)
{
logger.Error("发生异常,异常信息:" + ex.Message + ex.StackTrace);
}
}
}


Controller Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐