This article mainly introduces the ASP.net development of micro-trust public platform to obtain user information and processing of relevant information, the need for friends can refer to the
Get user Message
The message sent by the user is contained in an HTTP POST request sent by the micro-server to obtain the message sent by the user from the data stream of the POST request
Example of an HTTP request message that a micro-server pushes messages to a server
Post/cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6xtamp=1409659813&nonce=1372623149 HTTP/1.1
Host:qy.weixin.qq.com
Fetching data from a POST request
There are two possible scenarios for obtaining a user message: Encrypted message or unencrypted message, which is related to the selection of the message plus decryption mode when you configure the Web site on the micro-trust platform, if clear text mode is selected, it will not be encrypted, and if compatibility mode is selected, both ciphertext and plaintext exist, if Safe mode is selected, The user message is encrypted and needs to be decrypted before it can be processed further.
2. Reply User Message
Reference micro-credit common platform development Documentation
• Text messages
?
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> |
• Picture 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 is already there, and then we just need to set the appropriate 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 for you. ": Reply); |
3. Encryption and decryption of user messages and server messages
Micro-Credit public Platform developer documentation in the C++,c#,java and other languages such as encryption and decryption examples, we use C #, only two of them to add to the project can be, Sample.cs is the micro-letter team to give the sample code, do not need to reference, the
WXBizMsgCrypt.cs with the Cryptography.cs file to add a reference. To further encapsulate and facilitate the call, I have created a new class Wechatsecurityhelper
Class to define two methods, respectively, to encrypt (encryptmsg) and Decrypt (decryptmsg), create a Wxbizmsgcrypt object, invoke its method and decrypt, the specific code can be seen code sample
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 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>///definition token, consistent with token on the micro-trust platform/// summary> Private Const string Token = "Stupidme"; <summary>///AppId to be consistent with AppId on the micro-trust platform///</summary> Private Const string AppId = "11111111111"; <summary>///encryption with///</summary> Private Const string Aeskey = "Pvx2kzwrlqskuabvarglsaxcwttxgfwf3xonj9ieu MG "; 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>///encrypted message///</summary>///<paraM name= "MSG" > Message to encrypt </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>///decryption message///</summary>///<param name= "MSG" > Message body </param>///<returns> ; Clear text 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 the entire contents of this article, I hope you can enjoy.