Asp.net WeChat developer access skills

Source: Internet
Author: User
This article mainly introduces the developer access content in asp.net development. if you need it, refer to the following article to introduce the developer access content in asp.net development, for more information, see

First, let's take a look at the operations on the project:

Public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; string postString = string. empty; if (HttpContext. current. request. httpMethod. toUpper () = "POST") {using (Stream stream = HttpContext. current. request. inputStream) {Byte [] postBytes = new Byte [stream. length]; stream. read (postBytes, 0, (Int32) stream. length); postString = Encoding. UTF8.GetString (postBytes) ;} If (! String. isNullOrEmpty (postString) {ResponseXML (postString); // return the user information} // load the custom menu string postData = "{" + "\ r \ n "; postData + = "\" button \ ": [" + "\ r \ n"; postData + = "{" + "\ r \ n "; postData + = "\" name \ ": \" simple query \ "," + "\ r \ n"; postData + = "\" sub_button \": ["+" \ r \ n "; postData + =" {"+" \ r \ n "; postData + =" \ "type \": \ "click \", "+" \ r \ n "; postData + =" \ "name \": \ "My salary \", "+" \ r \ n "; postData + = "\" key \ ": \" mypay \ "" + "\ r \ n"; postData + = "}," + "\ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" weather forecast \ "," + "\ r \ n"; postData + = "\" key \": \ "tianqiyubao \" "+" \ r \ n "; postData + ="}, "+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" view \ "," + "\ r \ n "; postData + = "\" name \ ": \" train ticket query \ "," + "\ r \ n"; postData + = "\" url \":\" http://www.deqiaohr.com.cn/ *******. Aspx \ "" + "\ r \ n"; postData + = "}," + "\ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" happy moment \ "," + "\ r \ n"; postData + = "\" key \": \ "kaixinyixiao \" + "\ r \ n"; postData + = "}]" + "\ r \ n"; postData + = "}, "+" \ r \ n "; postData + =" {"+" \ r \ n "; postData + =" \ "name \": \ "member management \", "+" \ r \ n "; postData + =" \ "sub_button \": ["+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" view \ "," + "\ r \ n "; postData + = "\" name \ ": \" member registration \ "," + "\ r \ n"; postData + = "\" url \":\" http://www.deqiaohr.com.cn/ ****. Aspx \ "" + "\ r \ n"; postData + = "}," + "\ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" view \ "," + "\ r \ n "; postData + = "\" name \ ": \" Reset password \ "," + "\ r \ n"; postData + = "\" url \":\" http://www.deqiaohr.com.cn/ ****. Aspx \ "" + "\ r \ n"; postData + = "}," + "\ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" modify materials \ "," + "\ r \ n"; postData + = "\" key \": \ "updateMessage \" "+" \ r \ n "; postData + ="}, "+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" my questions \ "," + "\ r \ n"; postData + = "\" key \": \ "mywen \" "+" \ r \ n "; postData + ="}, "+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" contact customer service \ "," + "\ r \ n"; postData + = "\" key \": \ "PhoneSerices \" "+" \ r \ n "; postData + ="}] "+" \ r \ n "; postData + = "}, "+" \ r \ n "; postData + =" {"+" \ r \ n "; postData + =" \ "name \": \ "activity notification \", "+" \ r \ n "; postData + =" \ "sub_button \": ["+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" recent events \ "," + "\ r \ n"; postData + = "\" key \": \ "yuangonghuodong \" "+" \ r \ n "; postData + ="}, "+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" recent notifications \ "," + "\ r \ n"; postData + = "\" key \": \ "yuangongtongzhi \" "+" \ r \ n "; postData + ="}, "+" \ r \ n "; postData + = "{" + "\ r \ n"; postData + = "\" type \ ": \" click \ "," + "\ r \ n "; postData + = "\" name \ ": \" mandatory \ "," + "\ r \ n"; postData + = "\" key \": \ "youwenbida \" "+" \ r \ n "; postData + ="}] "+" \ r \ n "; postData + = "}]" + "\ r \ n"; postData + = "}" + "\ r \ n "; // the custom menu token is obtained using the following two parameters. the token string to = GetAccessToken () of the public platform cannot be used directly (); // I do not like the json operations in the background to directly intercept the token or retrieve the json token =. substring (17,. length-37); // load the menu string I = GetPage (" https://api.weixin.qq.com/cgi-bin/menu/create?access_token= "+ To, postData) ;}else {Auth (); // access test }}////// Get the pass //////
 Private string GetAccessToken () {string url_token = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ******** & secret = ********** "; HttpWebRequest myRequest = (HttpWebRequest) WebRequest. create (url_token); myRequest. method = "GET"; HttpWebResponse myResponse = (HttpWebResponse) myRequest. getResponse (); StreamReader reader = new StreamReader (myResponse. getResponseStream (), Encoding. UTF8); string content = reader. readToEnd (); reader. close (); return content ;}////// Load menu items ////////////
 Private string GetPage (string p, string postData) {Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; encoding encoding = Encoding. UTF8; byte [] data = encoding. getBytes (postData); // prepare the request... try {// set the parameter request = WebRequest. create (p) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer (); request. cookieContainer = cookieContainer; request. allowAutoRedirect = true; request. method = "POST"; request. contentType = "application/x-www-form-urlencoded"; request. contentLength = data. length; outstream = request. getRequestStream (); outstream. write (data, 0, data. length); outstream. close (); // send the request and obtain the response data response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request instream = response to the target webpage. getResponseStream (); sr = new StreamReader (instream, encoding); // return result webpage (html) code string content = sr. readToEnd (); string err = string. empty; return content;} catch (Exception ex) {string err = ex. message; return string. empty ;}}////// Obtain the parameter for authentication ///Private void Auth () {string token = "*****"; // The Token string echoString = HttpContext you entered when applying. current. request. queryString ["echoStr"]; string signature = HttpContext. current. request. queryString ["signature"]; string timestamp = HttpContext. current. request. queryString ["timestamp"]; string nonce = HttpContext. current. request. queryString ["nonce"]; if (CheckSignature (token, signature, timestamp, nonce )) {If (! String. IsNullOrEmpty (echoString) {HttpContext. Current. Response. Write (echoString); HttpContext. Current. Response. End ();}}}////// Encapsulate input parameters into an array, splice strings, and perform encryption //////////////////
 Private bool CheckSignature (string token, string signature, string timestamp, string nonce) {string [] ArrTmp = {token, timestamp, nonce}; // put the parameter into the Array. sort (ArrTmp); // Sort the array string tmpStr = string. join ("", ArrTmp); // concatenate arrays /// encrypt the concatenated strings tmpStr = FormsAuthentication. hashPasswordForStoringInConfigFile (tmpStr, "SHA1"); // converts it to lowercase tmpStr = tmpStr. toLower (); // if (tmpStr = signature) {return true ;}else {return false ;}}

The above is a detailed description of the access skills for asp.net developers. For more information, see other related articles on php Chinese network!

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.