Share. net tutorials on developing WeChat public platforms

Source: Internet
Author: User
Tags sha1 encryption
This article mainly introduces.. net to develop the public platform. net implementation methods have been described in more detail, very practical, need friends can refer to this article mainly share.. net development public platform instance tutorial, the principles of public platform development and the corresponding. net implementation methods are described in more detail, very practical, need friends can refer to the next

This document describes how to develop a public platform using. net. Share it with you for your reference. The specific implementation method is as follows:

I. description:

The public platform information interface provides developers with a new message processing method. you can use the development functions of the public platform only after you apply to become a developer, here, you need to enter a URL and a Token. you also need to have your own server (Internet server) resources. the Token can be entered by the developer at will, the URL is the link address of the interface configuration information. in this article, I use the method of creating a simple website and configure the relevant interface information in the background program of one of the pages, then publish it to the Internet server, and the link address that can access this page is the URL that should be filled in here.

II. Interface Configuration process:

1. website access-verify the signature:

The code is as follows:

Const string Token = "aka"; // defining a local variable cannot be modified. the variables defined here must be consistent with the Token entered in the interface configuration information. protected void Page_Load (object sender, eventArgs e) {string postStr = ""; Valid (); // verify the signature. after the entered information is submitted, the page prompts "you have successfully become a public platform developer, you can use the development function of the public platform. "In this case, you need to comment out the verification method to make the subsequent message responses work properly. if (Request. httpMethod. toLower () = "post") // when a common user sends a message to a public account, the server will POST the message to the filled URL {postStr = PostInput (); if (string. isNullOrEmpty (postStr) = false) {// Writ ELog (postStr, Server); // included in the log ResponseMsg (postStr) ;}} private void Valid () {string echoStr = Request. queryString ["echoStr"]. toString (); if (CheckSignature () {if (! String. IsNullOrEmpty (echoStr) {Response. Write (echoStr); Response. End ();}}}

The code is as follows:

////// Verify the signature //////
 Private bool CheckSignature () {string signature = Request. queryString ["signature"]. toString (); string timestamp = Request. queryString ["timestamp"]. toString (); string nonce = Request. queryString ["nonce"]. toString (); string [] ArrTmp = {Token, timestamp, nonce}; Array. sort (ArrTmp); // Sort string tmpStr = string in alphabetical order. join ("", ArrTmp); tmpStr = FormsAuthentication. hashPasswordForStoringInConfigFile (tmpStr, "SHA1"); // perform sha1 encryption on the string tmpStr = tmpStr. toLower (); // converts lowercase letters in a string. non-letter characters are not processed. // WriteLog (tmpStr, Server ); // if (tmpStr = signature) // The encrypted string obtained by the developer can be compared with signature to identify the Request source. The developer verifies the request by verifying signature. If you confirm that the GET request is from the server, the request returns the content of the echostr parameter as is. Otherwise, the access fails. {return true ;} else return false ;}////// Obtain the data returned by post //////
 Private string PostInput () {Stream s = System. web. httpContext. current. request. inputStream; byte [] B = new byte [s. length]; s. read (B, 0, (int) s. length); return Encoding. UTF8.GetString (B );}////// Response result //////Private void ResponseMsg (string weixinXML) {try {XmlDocument doc = new XmlDocument (); doc. loadXml (weixinXML); // read the XML string XmlElement rootElement = doc. documentElement; XmlNode MsgType = rootElement. selectSingleNode ("MsgType"); // Obtain the message type string resxml = ""; if (MsgType. innerText = "text") // if the message type is text message {var model = new {ToUserName = rootElement. selectSingleNode ("ToUserName "). innerText, FromUserName = rootElement. selectSingleNode ("FromUserName "). innerText, CreateTime = rootElement. selectSingleNode ("CreateTime "). innerText, MsgType = MsgType. innerText, Content = rootElement. selectSingleNode ("Content "). innerText, MsgId = rootElement. selectSingleNode ("MsgId "). innerText}; resxml + ="
 
  " + model.FromUserName + "
  
  " + model.ToUserName + "
  
  
   
"+ ConvertDateTimeInt (DateTime. Now) +"
  "; If (! String. isNullOrEmpty (model. content) // if the message is received {if (model. content. contains ("Hello") | model. content. contains ("good") | model. content. contains ("hi") | model. content. contains ("hello") // hello {resxml + ="
  text
  
  你好,有事请留言,偶会及时回复你的。
  
  
   
0
  
 ";}} Else // no message received {resxml + ="
 text
 
 亲,感谢您对我的关注,有事请留言。
 
 
  
0
 ";} Response. write (resxml);} if (MsgType. innerText = "image") // if the message type is image message {var model = new {ToUserName = rootElement. selectSingleNode ("ToUserName "). innerText, FromUserName = rootElement. selectSingleNode ("FromUserName "). innerText, CreateTime = rootElement. selectSingleNode ("CreateTime "). innerText, MsgType = MsgType. innerText, PicUrl = rootElement. selectSingleNode ("PicUrl "). innerText, MsgId = rootElement. selectSingleNode ("MsgId "). innerText}; resxml + ="
 
  " + model.FromUserName + "
  
  " + model.ToUserName + "
  
  
   
"+ ConvertDateTimeInt (DateTime. Now) +"
  
  news
  1
  
   <! [CDATA [Welcome!]>
   非常感谢您的关注!
   
   http://www.php.cn/
   
   http://www.php.cn/
   
  
  
   
0
  
 "; Response. write (resxml);} else // if it is another message type {var model = new {ToUserName = rootElement. selectSingleNode ("ToUserName "). innerText, FromUserName = rootElement. selectSingleNode ("FromUserName "). innerText, CreateTime = rootElement. selectSingleNode ("CreateTime "). innerText,}; resxml + ="
 
  " + model.FromUserName + "
  
  " + model.ToUserName + "
  
  
   
"+ ConvertDateTimeInt (DateTime. Now) +"
  
  text
  
  亲,感谢您对我的关注,有事请留言,我会及时回复你的哦。
  
  
   
0
  
 "; Response. Write (resxml) ;}} catch (Exception ex) {throw ex;} Response. End ();}////// Convert datetime to unixtime /////////
 Private int ConvertDateTimeInt (System. dateTime time) {System. dateTime startTime = TimeZone. currentTimeZone. toLocalTime (new System. dateTime (1970, 1, 1); return (int) (time-startTime ). totalSeconds ;}////// Write logs (used for tracking). you can include the content you want to print into a text file to facilitate testing ///Public static void WriteLog (string strMemo, HttpServerUtility server) {string filename = server. mapPath ("/logs/log.txt"); // create a folder named logs in the website project (then create a web page file in the folder, prevent the website from seeing the pre-file after publishing to the server.) if (! Directory. exists (server. mapPath ("// logs //") Directory. createDirectory ("// logs //"); StreamWriter sr = null; try {if (! File. exists (filename) {sr = File. createText (filename);} else {sr = File. appendText (filename);} sr. writeLine (strMemo);} catch {} finally {if (sr! = Null) sr. Close ();}}

The above is a detailed tutorial for sharing. net public platform examples. For more information, see other related articles on php Chinese network!

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.