Development of ASP. net mvc WeChat public platform: Getting and processing user messages, asp. netmvc

Source: Internet
Author: User

ASP. net mvc public platform development: Getting and processing user messages, asp. netmvc

ASP. net mvc public platform development

Obtain and process user messages

The message format already exists. Then we only need to set the corresponding parameters.

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

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Common 8 {9 public class WeChatSecurityHelper10 {11 /// <summary> 12 // define Token, consistent with the Token on the public platform 13 // </summary> 14 private const string Token = "StupidMe "; 15 /// <summary> 16 // AppId must be consistent with the AppId on the public platform 17 /// </summary> 18 private const string AppId = "1111 1111111 "; 19 /// <summary> 20 // encrypted with 21 /// </summary> 22 private const string AESKey =" encrypted "; 23 24 private static Tencent. WXBizMsgCrypt wxcpt = new Tencent. WXBizMsgCrypt (Token, AESKey, AppId); 25 private string signature, timestamp, nonce; 26 private static LogHelper logger = new LogHelper (typeof (WeChatSecurityHelper); 27 28 29 public WeChatSecurityHelper (string Signature, string timestamp, string nonce) 30 {31 this. signature = signature; 32 this. timestamp = timestamp; 33 this. nonce = nonce; 34} 35 36 // <summary> 37 // encrypt message 38 // </summary> 39 // <param name = "msg"> message to be encrypted </param> 40 // <returns> encrypted message </returns> 41 public string EncryptMsg (string msg) 42 {43 string encryptMsg = ""; 44 int result = wxcpt. encryptMsg (msg, timestamp, nonce, ref encryptMsg); 45 if (resu Lt = 0) 46 {47 return encryptMsg; 48} 49 else50 {51 logger. error ("message encryption failed"); 52 return ""; 53} 54} 55 56 // <summary> 57 // decrypt message 58 // </summary> 59 // <param name = "msg"> Message Body </param> 60 // <returns> plaintext message </returns> 61 public string DecryptMsg (string msg) 62 {63 string decryptMsg = ""; 64 int result = wxcpt. decryptMsg (signature, timestamp, nonce, msg, ref decryptMsg); 65 if (result! = 0) 66 {67 logger. Error ("message decryption failed, result:" + result); 68} 69 return decryptMsg; 70} 71} 72}WeChatSecurityHelper Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.