asp.net開發(fā)微信公眾平臺之驗(yàn)證消息的真實(shí)性
驗(yàn)證消息的真實(shí)性
在MVC Controller所在項(xiàng)目中添加過濾器,在過濾器中重寫
public override void OnActionExecuting(ActionExecutingContext filterContext)方法
新建數(shù)據(jù)模型

注:服務(wù)器接收消息時(shí),不再是signature而是msg_signature
微信服務(wù)器推送消息到服務(wù)器的HTTP請求報(bào)文示例
POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6×tamp=1409659813&nonce=1372623149 HTTP/1.1
Host: qy.weixin.qq.com
方法重寫,實(shí)現(xiàn)對消息的驗(yàn)證
調(diào)用微信接入時(shí)驗(yàn)證的方法,不過參數(shù)需要小改動一下,采用新建的數(shù)據(jù)模型


在Action方法或在Controller上添加過濾器屬性

代碼示例
Model
/// <summary>
/// 微信推送消息模型
/// </summary>
public class WeChatMsgRequestModel
{
public string timestamp { get; set; }
public string nonce { get; set; }
public string msg_signature { get; set; }
}
Filter
public class WeChatRequestValidAttribute : ActionFilterAttribute
{
private const string Token = "StupidMe";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//參數(shù)適配
Model.FormatModel.WeChatMsgRequestModel model = new Model.FormatModel.WeChatMsgRequestModel() { nonce= filterContext.HttpContext.Request.QueryString["nonce"],msg_signature= filterContext.HttpContext.Request.QueryString["msg_signature"],timestamp= filterContext.HttpContext.Request.QueryString["timestamp"] };
//驗(yàn)證
if (CheckSignature(model))
{
base.OnActionExecuting(filterContext);
}
}
private bool CheckSignature(Model.FormatModel.WeChatMsgRequestModel model)
{
string signature, timestamp, nonce, tempStr;
//獲取請求來的參數(shù)
signature = model.msg_signature;
timestamp = model.timestamp;
nonce = model.nonce;
//創(chuàng)建數(shù)組,將 Token, timestamp, nonce 三個(gè)參數(shù)加入數(shù)組
string[] array = { Token, timestamp, nonce };
//進(jìn)行排序
Array.Sort(array);
//拼接為一個(gè)字符串
tempStr = String.Join("", array);
//對字符串進(jìn)行 SHA1加密
tempStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tempStr, "SHA1").ToLower();
//判斷signature 是否正確
if (tempStr.Equals(signature))
{
return true;
}
else
{
return false;
}
}
}
Controller Code
/// <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")
{
//從請求的數(shù)據(jù)流中獲取請求信息
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("發(fā)生異常,異常信息:" + ex.Message + ex.StackTrace);
}
}
}
以上所述就是本文的全部內(nèi)容 了,希望大家能夠喜歡。
相關(guān)文章
ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探
這篇文章主要介紹了ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03
MVC文件上傳支持批量上傳拖拽及預(yù)覽文件內(nèi)容校驗(yàn)功能
這篇文章主要介紹了MVC文件上傳支持批量上傳拖拽及預(yù)覽文件內(nèi)容校驗(yàn)功能,需要的朋友可以參考下2017-03-03
Asp.Net 5分鐘實(shí)現(xiàn)網(wǎng)頁實(shí)時(shí)監(jiān)控
在項(xiàng)目開發(fā)中經(jīng)常會用到監(jiān)控功能,下面通過本篇文章給大家介紹Asp.Net 5分鐘實(shí)現(xiàn)網(wǎng)頁實(shí)時(shí)監(jiān)控,需要的朋友可以參考下2017-12-12
詳解mvc使用JsonResult返回Json數(shù)據(jù)
這篇文章主要介紹了詳解mvc使用JsonResult返回Json數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
ASP.NE網(wǎng)站發(fā)布注意事項(xiàng)簡析
ASP.NET網(wǎng)站部署到IIS中的關(guān)鍵點(diǎn),本文介紹了一些ASP.NE網(wǎng)站在發(fā)布是需要注意事項(xiàng),需要了解的朋友可以參考下2012-12-12
ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子...2007-07-07

