



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】C#開發(fā)微信之如何實(shí)現(xiàn)微信企業(yè)號(hào)消息和事件的接收處理及解密操作
1、企業(yè)號(hào)回調(diào)模式的設(shè)置/upload/information/20201208/260/14305.jpg
///
<summary>
///
企業(yè)號(hào)回調(diào)信息接口。統(tǒng)一接收并處理信息的入口。
///
</summary>
public
class
corpapi
:
IHttpHandler
{
///
<summary>
///
處理企業(yè)號(hào)的信息
///
</summary>
///
<param
name="context"></param>
public
void
ProcessRequest(HttpContext
context)
{
if
(HttpContext.Current.Request.HttpMethod.ToUpper()
==
"POST")
{
using
(Stream
stream
=
HttpContext.Current.Request.InputStream)
{
Byte[]
postBytes
=
new
Byte[stream.Length];
stream.Read(postBytes,
0,
(Int32)stream.Length);
postString
=
Encoding.UTF8.GetString(postBytes);
}
if
(!string.IsNullOrEmpty(postString))
{
Execute(postString,
accountInfo);
}
}
else
{
Auth(accountInfo);
}2、微信回調(diào)消息的驗(yàn)證驗(yàn)證URL有效性
#region
具體處理邏輯
string
echoString
=
HttpContext.Current.Request.QueryString["echoStr"];
string
signature
=
HttpContext.Current.Request.QueryString["msg_signature"];//企業(yè)號(hào)的
msg_signature
string
timestamp
=
HttpContext.Current.Request.QueryString["timestamp"];
string
nonce
=
HttpContext.Current.Request.QueryString["nonce"];
string
decryptEchoString
=
"";
if
(new
CorpBasicApi().CheckSignature(token,
signature,
timestamp,
nonce,
corpId,
encodingAESKey,
echoString,
ref
decryptEchoString))
{
if
(!string.IsNullOrEmpty(decryptEchoString))
{
HttpContext.Current.Response.Write(decryptEchoString);
HttpContext.Current.Response.End();
}
}
#endregion
///
<summary>
///
驗(yàn)證企業(yè)號(hào)簽名
///
</summary>
///
<param
name="token">企業(yè)號(hào)配置的Token</param>
///
<param
name="signature">簽名內(nèi)容</param>
///
<param
name="timestamp">時(shí)間戳</param>
///
<param
name="nonce">nonce參數(shù)</param>
///
<param
name="corpId">企業(yè)號(hào)ID標(biāo)識(shí)</param>
///
<param
name="encodingAESKey">加密鍵</param>
///
<param
name="echostr">內(nèi)容字符串</param>
///
<param
name="retEchostr">返回的字符串</param>
///
<returns></returns>
public
bool
CheckSignature(string
token,
string
signature,
string
timestamp,
string
nonce,
string
corpId,
string
encodingAESKey,
string
echostr,
ref
string
retEchostr)
{
WXBizMsgCrypt
wxcpt
=
new
WXBizMsgCrypt(token,
encodingAESKey,
corpId);
int
result
=
wxcpt.VerifyURL(signature,
timestamp,
nonce,
echostr,
ref
retEchostr);
if
(result
!=
0)
{
LogTextHelper.Error("ERR:
VerifyURL
fail,
ret:
"
+
result);
return
false;
}
return
true;
}3、企業(yè)號(hào)的消息處理
if
(HttpContext.Current.Request.HttpMethod.ToUpper()
==
"POST")
{
using
(Stream
stream
=
HttpContext.Current.Request.InputStream)
{
Byte[]
postBytes
=
new
Byte[stream.Length];
stream.Read(postBytes,
0,
(Int32)stream.Length);
postString
=
Encoding.UTF8.GetString(postBytes);
}
if
(!string.IsNullOrEmpty(postString))
{
Execute(postString,
accountInfo);
}
}
string
echoString
=
HttpContext.Current.Request.QueryString["echoStr"];
string
signature
=
HttpContext.Current.Request.QueryString["msg_signature"];//企業(yè)號(hào)的
msg_signature
string
timestamp
=
HttpContext.Current.Request.QueryString["timestamp"];
string
nonce
=
HttpContext.Current.Request.QueryString["nonce"];
//獲取配置參數(shù)并對(duì)加解密函數(shù)初始化
string
CorpToken
=
accountInfo.Token;
string
AESKey
=
accountInfo.EncodingAESKey;
string
CorpId
=
accountInfo.CorpID;
//根據(jù)參數(shù)信息,初始化微信對(duì)應(yīng)的消息加密解密類
WXBizMsgCrypt
wxcpt
=
new
WXBizMsgCrypt(CorpToken,
AESKey,
CorpId);
//對(duì)收到的密文進(jìn)行解析處理
string
sMsg
=
"";
//
解析之后的明文
int
flag
=
wxcpt.DecryptMsg(signature,
timestamp,
nonce,
postStr,
ref
sMsg);
if
(flag
==
0)
{
//LogTextHelper.Info("記錄解密后的數(shù)據(jù):");
//LogTextHelper.Info(sMsg);//記錄解密后的數(shù)據(jù)
CorpApiDispatch
dispatch
=
new
CorpApiDispatch();
string
responseContent
=
dispatch.Execute(sMsg);
//加密后并發(fā)送
//LogTextHelper.Info(responseContent);
string
encryptResponse
=
"";
timestamp
=
DateTime.Now.DateTimeToInt().ToString();
wxcpt.EncryptMsg(responseContent,
timestamp,
nonce,
ref
encryptResponse,
ref
signature);
HttpContext.Current.Response.ContentEncoding
=
Encoding.UTF8;
HttpContext.Current.Response.Write(encrypt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 餐飲店轉(zhuǎn)讓合同范例
- 勞務(wù)合同范例退休返聘
- 承包經(jīng)營(yíng)權(quán)轉(zhuǎn)讓的合同范例二零二五年
- 一圖讀懂新三板特定事項(xiàng)協(xié)議轉(zhuǎn)讓
- 心理咨詢師考試心理韌性培養(yǎng)試題及答案
- 干旱-復(fù)水條件下紅砂幼苗碳水動(dòng)態(tài)特征研究
- 公共健康教育對(duì)流動(dòng)人口身體健康水平的影響研究
- 母豬護(hù)理的系統(tǒng)化管理辦法試題及答案
- 九年級(jí)復(fù)課班英語教學(xué)工作總結(jié)(30篇)
- 資助工作者事跡(15篇)
- 手衛(wèi)生與無菌操作
- 寵物直播方案
- 骨科關(guān)節(jié)置換術(shù)后康復(fù)PDCA循環(huán)案例
- 腹腔引流管的護(hù)理查房
- 質(zhì)量經(jīng)理能力培訓(xùn)課件
- 光伏并網(wǎng)前單位工程驗(yàn)收?qǐng)?bào)告-2023
- 2024年重慶出版集團(tuán)招聘筆試參考題庫含答案解析
- 中國特色社會(huì)主義理論與實(shí)踐復(fù)習(xí)資料-研究生
- 【高中歷史】遼夏金元的統(tǒng)治課件-2024屆高三歷史統(tǒng)編版一輪復(fù)習(xí)
- 高速公路施工安全培訓(xùn)課件
- 幼兒行為觀察與分析案例教程 課件 第5、6章 幼兒情緒表現(xiàn)的觀察分析與指導(dǎo)、幼兒認(rèn)知發(fā)展的觀察分析與指導(dǎo)
評(píng)論
0/150
提交評(píng)論