Asp.net development public platform for obtaining and processing user messages
This article describes how to obtain and process user messages on the asp.net public platform. For more information, see
Get user messages
A user-sent message is contained in an http post request sent by the server. The user-sent message must be obtained from the data stream of the POST request.
Example of HTTP request message sent from the server to the server
POST/cgi-bin/wxpush? Msg_signature = 477715d11cdb4164915debcba66cb864d751f3e6 × tamp = 1409659813 & nonce = 1372623149 HTTP/1.1
Host: qy.weixin.qq.com
Get data from POST request
There are two possible user messages: Encrypted messages or Unencrypted messages, which are related to the selection of message encryption and decryption modes when you configure the website on the public platform, if the plaintext mode is selected, the message is not encrypted. If the compatibility mode is selected, the ciphertext and plaintext exist. If the security mode is selected, the user message is encrypted, decryption is required before further processing.
2. Reply to user messages
Refer to the public platform development documentation
• Text message
?
1 2 3 4 5 6 7 |
<Xml> <ToUserName> <! [CDATA [{0}]> </ToUserName> <FromUserName> <! [CDATA [{1}]> </FromUserName> <CreateTime >{2} </CreateTime> <MsgType> <! [CDATA [text]> </MsgType> <Content> <! [CDATA [{3}]> </Content> </Xml> |
• Image message
?
1 2 3 4 5 6 7 8 9 |
<Xml> <ToUserName> <! [CDATA [{0}]> </ToUserName> <FromUserName> <! [CDATA [{1}]> </FromUserName> <CreateTime >{2} </CreateTime> <MsgType> <! [CDATA [image]> </MsgType> <Image> <MediaId> <! [CDATA [{3}]> </MediaId> </Image> </Xml> |
The message format already exists. Then we only need to set the corresponding parameters.
?
1 2 3 4 5 |
ResponseContent = string. Format (ReplyType. Message_Text, FromUserName. InnerText, ToUserName. InnerText, DateTime. Now. Ticks, String. IsNullOrEmpty (reply )? "Sorry, I can not follow you.": reply ); |
3. encryption and decryption of user messages and server messages
The public platform developer documentation provides examples of encryption and decryption in c ++, C #, java, and other languages. We use C #, you only need to add two files to the project, Sample. cs is the sample code provided by the Team and does not need to be referenced.
Add reference to the WXBizMsgCrypt. cs and Cryptography. cs files. To further encapsulate and facilitate the call, I created a new WeChatSecurityHelper class.
The two methods are defined in the class, respectively for encryption (EncryptMsg) and decryption (DecryptMsg), create a WXBizMsgCrypt object, call its method for encryption and decryption, the specific code can be seen in the sample code
WeChatSecurityHelper
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
Using System; Using System. Collections. Generic; Using System. Linq; Using System. Text; Using System. Threading. Tasks; Namespace Common { Public class WeChatSecurityHelper { /// <Summary> /// Define the Token, which is consistent with the Token on the public platform. /// </Summary> Private const string Token = "StupidMe "; /// <Summary> /// AppId must be consistent with the AppId on the public platform /// </Summary> Private const string AppId = "11111111111 "; /// <Summary> /// Encryption /// </Summary> Private const string AESKey = "pvX2KZWRLQSkUAbvArgLSAxCwTtxgFWF3XOnJ9iEUMG "; Private static Tencent. WXBizMsgCrypt wxcpt = new Tencent. WXBizMsgCrypt (Token, AESKey, AppId ); Private string signature, timestamp, nonce; Private static LogHelper logger = new LogHelper (typeof (WeChatSecurityHelper )); Public WeChatSecurityHelper (string signature, string timestamp, string nonce) { This. signature = signature; This. timestamp = timestamp; This. nonce = nonce; } /// <Summary> /// Encrypt the message /// </Summary> /// <Param name = "msg"> message to be encrypted </param> /// <Returns> encrypted message </returns> Public string EncryptMsg (string msg) { String encryptMsg = ""; Int result = wxcpt. EncryptMsg (msg, timestamp, nonce, ref encryptMsg ); If (result = 0) { Return encryptMsg; } Else { Logger. Error ("message encryption failed "); Return ""; } } /// <Summary> /// Decrypt the message /// </Summary> /// <Param name = "msg"> message body </param> /// <Returns> plaintext message </returns> Public string DecryptMsg (string msg) { String decryptMsg = ""; Int result = wxcpt. DecryptMsg (signature, timestamp, nonce, msg, ref decryptMsg ); If (result! = 0) { Logger. Error ("message decryption failed, result:" + result ); } Return decryptMsg; } } } |
The above is all the content of this article. I hope you will like it.