Home  >  Article  >  WeChat Applet  >  C# development of WeChat portal and application configuration and use of WeChat enterprise account

C# development of WeChat portal and application configuration and use of WeChat enterprise account

高洛峰
高洛峰Original
2017-03-02 09:50:591627browse

At the beginning of this series of essays, we mainly introduce the development of portal applications for WeChat public accounts. Recently, the entire WeChat framework has been expanded and supplemented, adding the latest API encapsulation and development of enterprise accounts. The follow-up mainly introduces how to use C#. As for the development of WeChat Enterprise Account, this article serves as the starting point for the development of WeChat Enterprise Account and introduces the configuration and use of WeChat Enterprise Account.

1. Registration and login of WeChat enterprise account

Enterprise account is another type of WeChat after public account and subscription account. It is mainly for enterprises. Enterprise Account is the mobile application portal provided by WeChat for enterprise customers. It can help companies establish connections between employees, upstream and downstream supply chains, and corporate IT systems. Using the Enterprise Account, enterprises or third-party partners can help enterprises quickly and cost-effectively implement high-quality mobile light applications to realize the mobilization of production, management, collaboration, and operations.

Personally, I think the biggest highlight of Enterprise Account is that you can send an unlimited number of messages, which means you can communicate smoothly among employees of the enterprise. Compared with public accounts and subscription accounts, WeChat enterprise accounts are more cautious in sending messages. WeChat enterprise accounts can be said to be eye-catching. However, the WeChat enterprise account needs to establish an internal address book. Followers need to match the WeChat ID, email address, and phone number of the address book before they can follow it. This means that it can prevent other outsiders from following freely. In addition, for security reasons , you can also set up two-step verification, which is an audit process.

The certification of enterprise accounts is the same as that of public accounts. Relevant enterprise qualification documents need to be provided, and certification fees are charged every year. Otherwise, there may be some restrictions on personnel and functions. I feel like WeChat is really looking for ways to make money. The existing charging models include subscription accounts, public accounts, enterprise accounts, and open platforms. They all seem to have certification fees, and WeChat stores also need to collect a deposit of 20,000, everything. It's all money.

Okay, not much else to say. The registration address of WeChat is: https://qy.weixin.qq.com. You cannot register a WeChat official account and a WeChat enterprise account at the same time with one email address.

There are four steps required for an enterprise to open an enterprise account and start using it

1) The enterprise goes to the WeChat official website (http://qy.weixin.qq.com) to apply for activation;

2) After activation, the enterprise imports members in the enterprise account management background and publishes QR codes;

3) The enterprise calls the enterprise account API to connect with the enterprise’s own system for development;

4) Employees Follow, receive WeChat information, and interact with the company in WeChat

After registering the company account, you can scan the company QR code through WeChat to log in. When scanning, WeChat is required to confirm. Only then can you continue to enter your password to log in. The operation interface is as shown below (the left is a screenshot of the mobile phone, and the right is a screenshot of the web page).

C#开发微信门户及应用微信企业号的配置和使用 C#开发微信门户及应用微信企业号的配置和使用

After logging in, we can see the corresponding computer management interface.

C#开发微信门户及应用微信企业号的配置和使用

2. Set the development callback mode

If you have developed a WeChat official account, then we know that if you need to establish a WeChat public account between the WeChat server and the website server If the connection relationship is to realize the forwarding and processing of messages, then a callback mode should be set and the relevant parameters need to be configured. Then create an entrance in your own website server to process WeChat server messages.

C#开发微信门户及应用微信企业号的配置和使用

After entering the configuration, we need to modify the relevant parameters such as URL, Token, EncodingAESKey, etc., mainly the URL. This is the same as the entrance processing of the official account, and we need to publish to the processing entry on the website server.

Token and AESKey can be dynamically generated according to the prompts. AESKey seems to have to be 23 bits, so this is usually generated by itself. This is mainly used for encryption and decryption.

Explanation of the three parameters of URL, Token, and EncodingAESKey.

1) URL is the access protocol and address used by enterprise applications to receive push requests from enterprise accounts, and supports http or https protocols.

2) Token can be filled in arbitrarily by the enterprise and used to generate signatures.

3) EncodingAESKey is used to encrypt the message body and is the Base64 encoding of the AES key.

For detailed processing of verifying URL, Token and encryption, please refer to the subsequent "Encryption and Decryption Processing when Received Messages" section.

C#开发微信门户及应用微信企业号的配置和使用

The interface after configuring my company’s enterprise number is as follows.

C#开发微信门户及应用微信企业号的配置和使用

The page function pointed to in this URL needs to parse the data and return it to the WeChat server, so we need to pre-deploy this processing function entrance on the server.

In addition to the above functions, there is also a CorpID parameter that needs to be used, which we can view in the background main interface - Settings.

C#开发微信门户及应用微信企业号的配置和使用

Then in order to facilitate the use of the website backend, we put it in Web.Config the same as the official account configuration, as shown below.

C#开发微信门户及应用微信企业号的配置和使用

3. Implement the function development of the callback page

We have introduced several configuration items before, which need to be used in the callback page. This section continues to introduce how to Implement the postback of enterprise account information and make it pass the callback test operation.

Since the data of the callback test is sent through the Get method, our processing logic code is as follows, which is similar to the public account processing, but the implementation part is different.

    /// <summary>
    /// 企业号回调信息接口。统一接收并处理信息的入口。    /// </summary>
    public class corpapi : IHttpHandler
    {        /// <summary>
        /// 处理企业号的信息        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {            string postString = string.Empty;            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);
                }
            }            else
            {                Auth();
            }
        }        /// <summary>
        /// 成为开发者的第一步,验证并相应服务器的数据        /// </summary>
        private void Auth()
        {            #region 获取关键参数            string token = ConfigurationManager.AppSettings["CorpToken"];//从配置文件获取Token
            if (string.IsNullOrEmpty(token))
            {
                LogTextHelper.Error(string.Format("CorpToken 配置项没有配置!"));
            }            string encodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];//从配置文件获取EncodingAESKey
            if (string.IsNullOrEmpty(encodingAESKey))
            {
                LogTextHelper.Error(string.Format("EncodingAESKey 配置项没有配置!"));
            }            string corpId = ConfigurationManager.AppSettings["CorpId"];//从配置文件获取corpId
            if (string.IsNullOrEmpty(corpId))
            {
                LogTextHelper.Error(string.Format("CorpId 配置项没有配置!"));
            } 
            #endregion

            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();
                }
            }
        }

The specific processing code is as follows. One of the encryption and decryption processing classes is provided in the appendix of WeChat Enterprise Account. I used the C# version. Just SDK.

    /// <summary>
    /// 企业号基础操作API实现    /// </summary>
    public class CorpBasicApi : ICorpBasicApi
    {        /// <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;            
            //ret==0表示验证成功,retEchostr参数表示明文,用户需要将retEchostr作为get请求的返回参数,返回给企业号。            
            // HttpUtils.SetResponse(retEchostr);
        }

For more C# development of WeChat portal and application of WeChat enterprise account configuration and use, please pay attention to the PHP Chinese website for related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn