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

1、前言c#

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

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

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

      接口验证代码以下url

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,其他的参数为回调模式传递过来的参数。spa

下面是 CheckSignature 函数:.net

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为微信平台回调接口开发包,你们下载引用过来就好。code

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

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

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

相关文章
相关标签/搜索