星空网 > 软件开发 > ASP.net

.net之微信企业号开发(三) 回调模式的接口开发

一、前言

  微信企业号应用中,有两种模式,一种是普通模式,这种模式只能进行简单网页链接,以及发送固定的消息。为了可以让企业号的用户更好的与应用交互,微信提供了回调模式,这种回调模式的可以将用户发送给微信的信息,转发到用户提供的一个回调接口上,该接口解析用户发送过来的信息,解析后进行相应,而且回调模式中,可以调用的东西不少,扫码,图片,视频,地理位置信息等。

     在应用的模式下,选择回调模式,之后,需要设置3个参数(1.回调接口URL;2.token;3.ASESKey),URL就是提供的回调接口,微信会把用户提供的信息,转发到该接口来。我们这里用的url为http://m.xxx.com:10000/WeiXin/MessageInterface/。接口的验证是采用http Get的方式,接口获取消息是采用Post的方式。而且要设置回调模式,必须先实现验证接口。

这对接口的验证,微信提供了相应的开发包,有c#专用的,开发包的主要作用就是进行签名的验证和加密解密。

      接口验证代码如下

public string MessageInterface()    {      string signature = Request.QueryString["msg_signature"];      string timestamp = Request.QueryString["timestamp"];      string nonce = Request.QueryString["nonce"];      string echostr = Request.QueryString["echostr"];      //Careysoft.Basic.Public.Log.LogWrite("111");      if (Request.HttpMethod.ToUpper() == "GET") {        string decryptEchoString = "";        if (Careysoft.WeiXin.Public.WeiXinFunction.CheckSignature(m_Token, signature, timestamp, nonce, m_Corpid, m_EncodingAESKey, echostr, ref decryptEchoString))        {          if (!string.IsNullOrEmpty(decryptEchoString))          {            return decryptEchoString;          }        }      }}

m_Token为回调模式中设置的Token,m_Corpid为企业号微信Id,m_EncodingAESKey为回调模式中设置的AESKey,其余的参数为回调模式传递过来的参数。

下面是 CheckSignature 函数:

public static 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)      {        return false;      }      return true;      //ret==0表示验证成功,retEchostr参数表示明文,用户需要将retEchostr作为get请求的返回参数,返回给企业号。      // HttpUtils.SetResponse(retEchostr);    }

其中,WXBizMsgCrypt为微信平台回调接口开发包,大家下载引用过来就好。

好的,回调验证接口就是这样,有了这个,就可以把应用模式设置为回调模式了!

  下一节,我们将试着从回调接口接收用户发送的信息,并对用户进行回复。

       请看.net之微信企业号开发(四) 回调模式的接口 信息的接收和发送




原标题:.net之微信企业号开发(三) 回调模式的接口开发

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流