Senparc. Weixin. MPSDK WeChat public platform development tutorial (3): WeChat public platform development verification

Source: Internet
Author: User
To connect to the public platform's & quot; development model & quot;, that is, to connect to your own website program, you must register it after successful (see Senparc. weixin. MPSDK public platform development tutorial (1): Public platform registration). wait for the official review. after the application is approved, the "advanced functions" menu will appear on the top of the background. To connect to the "development mode" of the public platform, you must register your website program (see Senparc. weixin. mp sdk public platform development tutorial (1): Public platform registration). wait for the official review. after the application is approved, the "advanced functions" menu will appear at the top of the background.

Before using "advanced functions"> "development mode", you must have a website that has been deployed on the Internet and can be accessed through Port 80 (either by domain name or IP address ), for some development preparations, see Senparc. weixin. mp sdk public platform development tutorial (2): become a developer

After entering "advanced functions" for the first time, the interface is as follows:

At the time of submission, the server will use the Token for a Get request verification for the entered URL. if the request passes successfully, it is saved successfully. if the request fails, an error message is displayed, and the entered information cannot be saved.

To demonstrate more intuitively what elements should be included in the program corresponding to this URL, I will provide the implementation method of Senparc. Weixin. MP (MVC and WebForms will be used as examples respectively ):

Method 1: use MVC

1. create a Controller, such as WeixinController. cs, reference Senparc. weixin. MP. dll (using Senparc. weixin. MP. dll). For the latest DLL, see Senparc. weixin. MP. buildOutPut folder https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Senparc.Weixin.MP.BuildOutPut

2. set a private variable (you can also save it in the database), for example:

Public readonly string Token = "weixin"; // it is case sensitive and consistent with the Token settings in the background of the public account.

3. create an Action named Index for Get requests, such:

////// Backend authentication address (use Get), enter the "interface configuration information" Url in the background such as: http://www.php.cn ////[HttpGet] [ActionName ("Index")] public ActionResult Get (string signature, string timestamp, string nonce, string echostr) {if (CheckSignature. check (signature, timestamp, nonce, Token) {return Content (echostr); // if a random string is returned, it indicates that the verification has passed} else {return Content ("failed: "+ signature +", "+ MP. checkSignature. getSignature (timestamp, nonce, Token) + ". If you see this information in your browser, this Url can be entered in the background. ");}}

At this point, the code is compiled and deployed on the website as required, and fill in http://www.php.cn/in "interface configuration information", tokenfill in weixin.

For the complete WeixinController. cs code, see here.

Method 2: Use Web Forms

The verification process of Web Forms is the same as that of MVC, but the writing method is a little different. here, the code (also using Senparc) is provided directly. weixin. MP. dll). assume that the file name is weixin under the root directory. aspx:

Public partial class Weixin: System. Web. UI. Page {private readonly string Token = "weixin"; // consistent with the Token settings in the public account background, case sensitive. Protected void Page_Load (object sender, EventArgs e) {string signature = Request ["signature"]; string timestamp = Request ["timestamp"]; string nonce = Request ["nonce"]; string echostr = Request ["echostr"]; if (Request. httpMethod = "GET") {// get method-triggers if (CheckSignature. check (signature, timestamp, nonce, Token) {WriteContent (echostr); // if a random string is returned, it indicates that the verification has passed} else {WriteContent ("failed:" + signature + ", "+ CheckSignature. getSignature (timestamp, nonce, Token) ;}} else {// Determine Post or other request methods} Response. end ();} private void WriteContent (string str) {Response. output. write (str );}}

According to the above code, in "interface configuration information", the Url should be filled in http: // YourDomain/weixin. aspx, and the Token should be filled in weixin.

See the complete weixin. aspx. cs code above (of course you can also write it as ashx to further improve efficiency ).

The CheckSignature. Check () method is a method used to verify the request in the Senparc. Weixin. mp sdk and has been encapsulated. It is directly used here to simplify the code and highlight the key points. interested friends can directly view the source code.

Any of the above two methods, MVC and Web Forms, can be used for background verification. However, note that there is a Get method and it cannot communicate with the user, because we know through the previous tutorial that the background Url is requested through the Get method, client requests are Post requests. I will describe the Post method in detail in the following tutorial.

In addition, in the current setting status, even if you write the Post-related method, after verification, it does not mean that your server can already receive messages from the server. The last step is as follows: manually enable "developer mode ":

Now this account has successfully switched to "developer mode". all messages sent from clients to this public account will be forwarded to the Url just filled in.

In the next article, we will introduce how to respond to Post requests sent from the client in the most "coarse-grained" way.

After learning about the "rough mining" method, we will officially enter the "exquisite" and "concise" Senparc. Weixin. mp sdk method.

More Senparc. Weixin. mp sdk public platform development tutorial (3): Public platform development verification related articles, please follow the 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.