【移动应用开发技术】C#开发微信之如何实现微信企业号消息和事件的接收处理及解密操作_第1页
【移动应用开发技术】C#开发微信之如何实现微信企业号消息和事件的接收处理及解密操作_第2页
【移动应用开发技术】C#开发微信之如何实现微信企业号消息和事件的接收处理及解密操作_第3页
【移动应用开发技术】C#开发微信之如何实现微信企业号消息和事件的接收处理及解密操作_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论