


C# development of WeChat portals and applications - reception, processing and decryption of messages and events from WeChat enterprise accounts
In the previous essay "C# Development of WeChat Portal and Application (19) - Messaging with WeChat Enterprise Account (Text, Pictures, Files, Voice, Video, Graphic Messages, etc.)" introduced the message sending with Enterprise Account, Officials specifically state that messages are sent without encryption. But on the callback server, which is the server of our website, the messages sent from WeChat are encrypted, and we need to call the class library to decrypt the messages and events. Since the official examples are incomplete, we spent a lot of time exploring. Finally, various messages and events received were successfully decrypted. This article mainly introduces the reception, processing and decryption operations of messages and events on WeChat Enterprise Accounts.
1. Setting the callback mode of the enterprise account
Like the public account, if the WeChat enterprise account needs to be developed for the second time, the corresponding callback parameters need to be set in the background, as shown in the following interface .
After setting these and passing the check, we can send and receive messages on our WeChat application server.
At the callback message entry, we need to process POST data and ordinary GET data separately. GET data is WeChat's own verification processing, and POST data is the interactive operation of WeChat messages.
/// <summary> /// 企业号回调信息接口。统一接收并处理信息的入口。 /// </summary> public class corpapi : IHttpHandler { /// <summary> /// 处理企业号的信息 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) {
Above we defined a general application handler to process messages.
Then we separate different message types (POST, GET methods) and process them accordingly.
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, accountInfo); } } else { Auth(accountInfo); }
2. Verification of WeChat callback messages
The following is WeChat’s description of callback mode and verification URL.
Verify URL validity
When you submit the above information, the enterprise account will send a GET request to the filled-in URL. The GET request carries four parameters,Enterprises need to perform urldecode processing when obtaining , otherwise the verification will fail.
Parameter | Description | Is it required |
---|---|---|
msg_signature | WeChat encrypted signature, msg_signature combines the token filled in by the enterprise, the timestamp, nonce parameter in the request, and the encrypted message body | is |
timestamp | Timestamp | is |
nonce | Random number | is |
Encrypted random string, provided in msg_encrypt format. It is necessary to decrypt and return the echostr plaintext. After decryption, there are four fields: random, msg_len, msg, and $CorpID. msg is the echostr plaintext. | must be included in the first verification. |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

