コード例 #1
0
 /// <summary>
 /// 微信后台消息POST处理函数
 /// </summary>
 /// <param name="id">微信公众号ID,系统可以托管多个公众号,用于区分各个公众号</param>
 /// <param name="signature">微信后台发过来的签名参数,用于验证是否是微信服务器发的消息</param>
 /// <param name="timestamp">微信后台发过来的签名参数,用于验证是否是微信服务器发的消息</param>
 /// <param name="nonce">微信后台发过来的签名参数,用于验证是否是微信服务器发的消息</param>
 /// <param name="msg">微信POST请求中的消息主体内容,后台会自动用XMLFORMATER反序列化</param>
 /// <returns></returns>
 public HttpResponseMessage Post(string id, string signature, string timestamp, string nonce, WXRequestMsg msg)
 {
     try
     {
         var wx = WXManager.Current[id];
         //检查签名
         if (wx == null || wx.GetSigniture(timestamp, nonce).ToUpper() != signature.ToUpper())
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
         if (msg != null)
         {
             var ret = new WXRequestHandler(this.Request).HandleMsg(id, msg);
             if (ret != null)
             {
                 var response = Request.CreateResponse();
                 response.Content = new StringContent(ret.ToXML());
                 return(response);
             }
             return(Request.CreateResponse());
         }
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new HttpError("消息不能解析")));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, new HttpError(ex.Message)));
     }
 }
コード例 #2
0
 /// <summary>
 /// 微信后台消息POST处理函数
 /// </summary>
 /// <param name="id">微信公众号ID,系统可以托管多个公众号,用于区分各个公众号</param>
 /// <param name="msg_signature">微信后台发过来的签名参数,用于验证是否是微信服务器发的消息</param>
 /// <param name="timestamp">微信后台发过来的签名参数,用于验证是否是微信服务器发的消息</param>
 /// <param name="nonce">微信后台发过来的签名参数,用于验证是否是微信服务器发的消息</param>
 /// <param name="msg">微信POST请求中的消息主体内容,后台会自动用XMLFORMATER反序列化</param>
 /// <returns></returns>
 public HttpResponseMessage Post(string id, string msg_signature, string timestamp, string nonce)
 {
     try
     {
         var wx = WXManager.Current[id];
         if (wx != null)
         {
             var    wxcpt   = new Tencent.WXBizMsgCrypt(wx.Token, wx.EncodingAESKey, wx.AppID);
             var    content = Request.Content.ReadAsStringAsync().Result;
             string xml     = ""; // 解析之后的明文
             var    ret     = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, content, ref xml);
             if (ret == 0)
             {
                 var          stream = new StringReader(xml);
                 WXRequestMsg msg    = new XmlSerializer(typeof(WXRequestMsg)).Deserialize(stream) as WXRequestMsg;
                 if (msg != null)
                 {
                     var res = new WXRequestHandler(this.Request).HandleMsg(id, msg);
                     if (res != null)
                     {
                         var    response    = Request.CreateResponse();
                         string sRespData   = res.ToXML();
                         string sEncryptMsg = ""; //xml格式的密文
                         ret = wxcpt.EncryptMsg(sRespData, timestamp, nonce, ref sEncryptMsg);
                         if (ret == 0)
                         {
                             response.Content = new StringContent(sEncryptMsg);
                             return(response);
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
     }
     return(Request.CreateResponse());
 }