8、推送component_verify_ticket协议
在公众号第三方平台创建审核通过后,微信服务器会向其“授权事件接收URL”每隔10分钟定时推送component_verify_ticket。第三方平台方在收到ticket推送后也需进行解密(详细请见【消息加解密接入指引】),接收到后必须直接返回字符串success。
上面是微信API里的原话,在回调页面做了下日志记录:
string post = ""; string get = ""; for (int i = 0; i < Request.Form.Count; i++) { post += Request.Form.Keys[i] + "=" + Request.Form[i] + " "; } for (int i = 0; i < Request.QueryString.Count; i++) { get += Request.QueryString.Keys[i] + "=" + Request.QueryString[i] + " "; }</span>但是,日志记录的Post和Get参数,都没内容 component_verify_ticket 相关的内容。
后面经过一番查询,原来是要这样才能获取到微信推送的内容:
byte[] data = Request.BinaryRead(Request.TotalBytes);string postData = Encoding.Default.GetString(data);
//公众号第三方平台的appid
string appId = ConfigurationManager.AppSettings["WeixinAppID"];
//第三方平台申请时填写的接收消息的校验token
string token = ConfigurationManager.AppSettings["WeixinToken"];
//第三方平台申请时填写的接收消息的加密symmetric_key
string encodingAesKey = ConfigurationManager.AppSettings["WeixinEncodingAESKey"];
string sMsg = "";//解密后的内容
var msg = new WXBizMsgCrypt(token, encodingAesKey, appId);
int ret = msg.DecryptMsg(
Request.QueryString[" msg_signature "],
Request.QueryString["timestamp"],
Request.QueryString["nonce"],
postData,
ref sMsg);
AppService.LogService.Log("回调参数", "构造参数:" +
" appId:" + appId +
" token:" + token +
" encodingAesKey:" + encodingAesKey + " 解密参数:" +
" signature:" + Request.QueryString["signature"] +
" msg_signature:" + Request.QueryString["msg_signature"] +
" timestamp:" + Request.QueryString["timestamp"] +
" nonce:" + Request.QueryString["nonce"] +
" postData:" + postData
);
AppService.LogService.Log("解密结果", ret + " " + sMsg);
解密后得到的结果大概是这样:
<xml> <AppId> <![CDATA[wx34b5f92cdbb24fe4]]> </AppId> <CreateTime>1436066428</CreateTime> <InfoType> <![CDATA[component_verify_ticket]]> </InfoType> <ComponentVerifyTicket> <![CDATA[ticket@@@SfS2Ck4oxPhL0LRSJH5v9Pr68vjeDfWWPx9SzIWzm6PeKGeEoszohQ-5-EBm4RajI7AKlzIz1Kj1ghPg0t199w]]> </ComponentVerifyTicket> </xml>
ComponentVerifyTicket节点即是我们需要的结果。
希望遇到同样问题的童鞋不用走弯路。