Asp. NET micro-credit Development (interface Guide) _ Practical Tips

Source: Internet
Author: User
Tags sha1 sha1 encryption

Once the public platform user submits the information, the micro-server sends a GET request to the completed URL with four parameters:

The developer verifies the request by checking the signature (there is a check method below). If you confirm that this GET request comes from a micro-trust server, return the ECHOSTR parameter content as is, the access is valid, or the access fails.

Signature combines the token parameters and timestamp parameters and Nonce parameters that are filled in by the developer.

Encryption/validation Process:

    • 1. Sort token, timestamp, nonce three parameters in dictionary order
    • 2. Concatenation of three parameter strings into a string for SHA1 encryption
    • 3. The developer obtains the encrypted string to compare with the signature, identifies the request from the micro-letter
<summary>///Verification Signature///</summary>///<param name= "signature" ></param>///<param 
 Name= "timestamp" ></param>///<param name= "nonce" ></param>///<returns></returns> public static bool Checksignature (string signature, string timestamp, string nonce) {string[] arr = new string[] {T 
 Oken, timestamp, nonce}; 
 
 The token, timestamp, nonce three parameters are sorted in dictionary order array.sort<string> (arr); 
 StringBuilder content = new StringBuilder (); for (int i = 0; i < arr. Length; i++) {content. 
 Append (Arr[i]); String tmpstr = sha1_encrypt (content. 
 
 
 ToString ()); The SHA1 encrypted string can be compared with signature to identify the request from the micro-mail return tmpstr!= null? 
 Tmpstr.equals (signature): false; ///<summary>///Use default key to encrypt string///</summary>///<param name= "source_string" &GT;&LT;/PARAM&G 
 T <returns></returns> public static string Sha1_encrypt (String source_string) {byte[] strres = EncodiNg. 
 Default.getbytes (source_string); 
 HashAlgorithm Isha = new SHA1CryptoServiceProvider (); 
 Strres = Isha.computehash (strres); 
 StringBuilder entext = new StringBuilder (); 
 foreach (Byte ibyte in strres) {Entext.appendformat ("{0:x2}", Ibyte); 
 return entext.tostring ();  }

After the access is a message push when ordinary micro-trust users to the public account message, the micro-trust server will post the message to the completed URL.

 protected void Page_Load (object sender, EventArgs e) {if (Request.HttpMethod.ToUpper () = = "Get") {//micro-letter encryption Signature 
  String signature = request.querystring["signature"]; 
  Timestamp string timestamp = request.querystring["timestamp"]; 
  Random number string nonce = request.querystring["nonce"]; 
  Random string echostr = request.querystring["Echostr"]; 
  if (weixinserver.checksignature (signature, timestamp, nonce)) {Response.Write (ECHOSTR); } else if (Request.HttpMethod.ToUpper () = "POST") {StreamReader stream = new StreamReader (request.inputst 
  Ream); string xml = Stream. 
 
  ReadToEnd (); 
 ProcessRequest (XML); 
 }///<summary>///processing requests for micro-letters///</summary>///<param name= "xml" ></param> 
 
  public void ProcessRequest (String xml) {try {//XML request Resolution Hashtable REQUESTHT = Weixinserver.parsexml (XML); 
  The sender account (open_id) String fromusername = (string) requestht["Fromusername"]; 
Public account  String tousername = (string) requestht["Tousername"]; 
 
  Message type String msgtype = (string) requestht["Msgtype"]; 
 
  Text message if (Msgtype = = Reqmsgtype.text) {//Response.Write (str); 
  String content = (string) requestht["content"]; 
   if (content== "1") {//Response.Write (str); 
   Response.Write (Getnewsmessage (Tousername, fromusername)); 
  Return 
   } if (content = = "2") {Response.Write (Getuserblogmessage (Tousername, fromusername)); 
  Return 
   } if (content = = "3") {Response.Write (Getgroupmessage (Tousername, fromusername)); 
  Return 
   } if (content = = "4") {Response.Write (Getwinepartymessage (Tousername, fromusername)); 
  Return 
 
  Response.Write (Getmainmenumessage (Tousername, Fromusername, "Hello, I am Vinehoo,")); 
  else if (Msgtype = = reqmsgtype.event) {//Event type String EventType = (string) requestht["event"]; Subscribe to the IF (Eventtype==reqeventtype.subscribe) {Response.Write (getmainmenumessAge (Tousername, fromusername, "Thank you for your attention!") 
   
  ,")); }//Unsubscribe else if (eventtype==reqeventtype.unsubscribe) {//TODO) unsubscribe after the user does not receive the public number to send the message, so do not need to reply to the message}//Custom The semantic menu click event Else if (Eventtype==reqeventtype.click) {//TODO custom menu Right is not open, temporarily do not handle the message}} else if (Msgtype = R  eqmsgtype.location) {}} catch (Exception e) {}}<pre name= "code" class= "CSharp" > Protected void Page_Load (object sender, EventArgs e) {if (Request.HttpMethod.ToUpper () = = "Get") {//micro-letter encrypted signature string 
  Signature = request.querystring["signature"]; 
  Timestamp string timestamp = request.querystring["timestamp"]; 
  Random number string nonce = request.querystring["nonce"]; 
  Random string echostr = request.querystring["Echostr"]; 
  if (weixinserver.checksignature (signature, timestamp, nonce)) {Response.Write (ECHOSTR); } else if (Request.HttpMethod.ToUpper () = "POST") {StreamReader stream = new StreamReader (request.inputst Ream); string xml = Stream. 
 
  ReadToEnd (); 
 ProcessRequest (XML); 
 }///<summary>///processing requests for micro-letters///</summary>///<param name= "xml" ></param> 
 
  public void ProcessRequest (String xml) {try {//XML request Resolution Hashtable REQUESTHT = Weixinserver.parsexml (XML); 
  The sender account (open_id) String fromusername = (string) requestht["Fromusername"]; 
  Public account String tousername = (string) requestht["Tousername"]; 
 
  Message type String msgtype = (string) requestht["Msgtype"]; 
 
  Text message if (Msgtype = = Reqmsgtype.text) {//Response.Write (str); 
  String content = (string) requestht["content"]; 
   if (content== "1") {//Response.Write (str); 
   Response.Write (Getnewsmessage (Tousername, fromusername)); 
  Return 
   } if (content = = "2") {Response.Write (Getuserblogmessage (Tousername, fromusername)); 
  Return 
   } if (content = = "3") {Response.Write (Getgroupmessage (Tousername, fromusername)); 
Return  } if (content = = "4") {Response.Write (Getwinepartymessage (Tousername, fromusername)); 
  Return 
 
  Response.Write (Getmainmenumessage (Tousername, Fromusername, "Hello, I am Vinehoo,")); 
  else if (Msgtype = = reqmsgtype.event) {//Event type String EventType = (string) requestht["event"]; Subscribe to the IF (eventtype==reqeventtype.subscribe) {Response.Write Getmainmenumessage (tousername, FromUserName, " Thank you for your attention! 
   
  ,")); }//Unsubscribe else if (eventtype==reqeventtype.unsubscribe) {//TODO) unsubscribe after the user does not receive the public number to send the message, so do not need to reply to the message}//Custom The semantic menu click event Else if (Eventtype==reqeventtype.click) {//TODO custom menu Right is not open, temporarily do not handle the message}} else if (Msgtype = R 
eqmsgtype.location) {}} catch (Exception e) {}}</pre><br> <pre></pre>  <br> <br>

This article has been sorted out to the ASP. NET micro-Credit Development tutorial Summary, you are welcome to learn to read.

The above is about ASP.net micro-letter Development Interface Guide related content Introduction, hope for everyone's learning help.

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.