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

注:服務(wù)器接收消息時,不再是signature而是msg_signature
微信服務(wù)器推送消息到服務(wù)器的HTTP請求報文示例
POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6×tamp=1409659813&nonce=1372623149 HTTP/1.1
Host: qy.weixin.qq.com
方法重寫,實(shí)現(xiàn)對消息的驗證
調(diào)用微信接入時驗證的方法,不過參數(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"] };
//驗證
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 三個參數(shù)加入數(shù)組
string[] array = { Token, timestamp, nonce };
//進(jìn)行排序
Array.Sort(array);
//拼接為一個字符串
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)文章
深入Lumisoft.NET組件與.NET API實(shí)現(xiàn)郵件發(fā)送功能的對比分析
本篇文章對Lumisoft.NET組件與.NET API實(shí)現(xiàn)郵件發(fā)送的功能兩者進(jìn)行了深入的對比分析。需要的朋友參考下2013-05-05
關(guān)于兩個自定義控件的取值問題及接口的應(yīng)用
一個.aspx的頁面中,用到了兩個用戶控件,其中想做的到A控件有一個按鈕,點(diǎn)擊的時候獲取到B控件中的一個textbox的值想必大家會使用findcontrol獲取控件吧,而在生成的時候名字是不確定的,那么如何書寫呢?接下來為您提供詳細(xì)的解決方法,感興趣的朋友可以了解下啊2013-01-01
ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
asp.net 安全的截取指定長度的html或者ubb字符串
在將html代碼輸出到頁面時,有時候會需要截斷字符串保留指定長度的字符串,由于html中有些標(biāo)簽必須成對出現(xiàn),所以在截取html時需要特別注意,不能因為截斷問題把頁面搞亂掉。2010-01-01
在 ASP.Net Core 中使用 MiniProfiler的方法
這篇文章主要介紹了在 ASP.Net Core 中使用 MiniProfiler的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

