



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】C#开发微信之如何实现微信企业号消息和事件的接收处理及解密操作
1、企业号回调模式的设置/upload/information/20201208/260/14305.jpg
///
<summary>
///
企业号回调信息接口。统一接收并处理信息的入口。
///
</summary>
public
class
corpapi
:
IHttpHandler
{
///
<summary>
///
处理企业号的信息
///
</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、微信回调消息的验证验证URL有效性
#region
具体处理逻辑
string
echoString
=
HttpContext.Current.Request.QueryString["echoStr"];
string
signature
=
HttpContext.Current.Request.QueryString["msg_signature"];//企业号的
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>
///
验证企业号签名
///
</summary>
///
<param
name="token">企业号配置的Token</param>
///
<param
name="signature">签名内容</param>
///
<param
name="timestamp">时间戳</param>
///
<param
name="nonce">nonce参数</param>
///
<param
name="corpId">企业号ID标识</param>
///
<param
name="encodingAESKey">加密键</param>
///
<param
name="echostr">内容字符串</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、企业号的消息处理
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"];//企业号的
msg_signature
string
timestamp
=
HttpContext.Current.Request.QueryString["timestamp"];
string
nonce
=
HttpContext.Current.Request.QueryString["nonce"];
//获取配置参数并对加解密函数初始化
string
CorpToken
=
accountInfo.Token;
string
AESKey
=
accountInfo.EncodingAESKey;
string
CorpId
=
accountInfo.CorpID;
//根据参数信息,初始化微信对应的消息加密解密类
WXBizMsgCrypt
wxcpt
=
new
WXBizMsgCrypt(CorpToken,
AESKey,
CorpId);
//对收到的密文进行解析处理
string
sMsg
=
"";
//
解析之后的明文
int
flag
=
wxcpt.DecryptMsg(signature,
timestamp,
nonce,
postStr,
ref
sMsg);
if
(flag
==
0)
{
//LogTextHelper.Info("记录解密后的数据:");
//LogTextHelper.Info(sMsg);//记录解密后的数据
CorpApiDispatch
dispatch
=
new
CorpApiDispatch();
string
responseContent
=
dispatch.Execute(sMsg);
//加密后并发送
//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等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 科技园区场地合作经营与管理协议书范本
- 餐厅装修及设备采购安装监理合同
- 软件开发采购合同知识产权保护及售后服务协议
- 书法创新活动方案
- 义捐活动策划方案
- 副科干部宿舍管理制度
- 农林牧废弃物综合利用项目环境影响评价报告
- 儿科护理急性感染性喉炎
- 学校联合教研管理制度
- 厨房过失扣分管理制度
- 工程测试技术试题及答案
- 2025年下半年湖南永州蓝山县事业单位招聘工作人员38人易考易错模拟试题(共500题)试卷后附参考答案
- 火锅店员工合同协议书
- 企业如何通过激励措施促进员工参与数字化转型
- 雇人包工免责协议书
- 船舶应急部署表及船员应变卡
- 2025年下半年山东能源集团权属企业内蒙古荣信化工限公司社会招聘易考易错模拟试题(共500题)试卷后附参考答案
- 陕西邮政校招笔试题及答案
- 山东省济南市2025届高三三模历史试卷(含答案)
- 小学语文大单元整体教学设计讲座
- 风力发电场调试规程
评论
0/150
提交评论