ASP. NET Web API for WeChat public platform development (1), asp. netapi

Source: Internet
Author: User

ASP. NET Web API for public platform development (1), asp. netapi

Recently, my friend's public account was preparing for an activity, but it couldn't be done with a fixed public platform template. So please use it as the development backend. As I tried to develop the backend for the first time, I also made a lot of pitfalls. This series of blogs will describe the implementation of various functions of the public account.

 

Prerequisites

1. a server or cloud platform that can deploy web Services (I use Microsoft Azure)

2. A public account that can be used normally

3. Visual Studio

 

Development Preparation

1. Use ASP. NET Web API website project as the public account Background Service

 

Become a public account developer

 

This step is very simple, as long as the Developer Center at the public account backend agrees to the agreement, after the consent page is as follows:

 

Server Configuration

 

Log on to the official website of the public platform, go to the background management page of the public platform-Developer Center page, click the "modify configuration" button, and enter the server address (URL), Token, and EncodingAESKey, the URL is the interface URL used by the developer to receive messages and events. The Token can be entered by the developer as needed to generate a signature (the Token will be compared with the Token contained in the interface URL to verify security ). The EncodingAESKey is manually entered or randomly generated by the developer and will be used as the accesskey for message body encryption and decryption.

In addition, developers can select the plaintext mode, compatibility mode, and security mode for message encryption and decryption. The Mode Selection and server configuration will take effect immediately after submission. Please be careful when entering and selecting. The default encryption/Decryption mode is plain text. To select the compatible mode and security mode, you must configure the encryption/decryption code in advance.

 

// Note that the returned echostr string type only accepts "application/x-www-form-urlencoded" and directly returns public HttpResponseMessage Get (string signature, string timestamp, string nonce, string echostr) {if (CheckSource (signature, timestamp, nonce) {var result = new StringContent (echostr, UTF8Encoding. UTF8, "application/x-www-form-urlencoded"); var response = new HttpResponseMessage {Content = result}; return response;} return new HttpResponseMessage ();} // check whether the signature public bool CheckSource (string signature, string timestamp, string nonce) {var str = string. empty; var token = "cwtesttoken"; var parameter = new List <string> {token, timestamp, nonce}; parameter. sort (); var parameterStr = parameter [0] + parameter [1] + parameter [2]; var tempStr = GetSHA1 (parameterStr ). replace ("-",""). toLower (); if (tempStr = signature) return true; return false;} // encrypt public string GetSHA1 (string input) {var output = string. empty; var sha1 = new SHA1CryptoServiceProvider (); var inputBytes = UTF8Encoding. UTF8.GetBytes (input); var outputBytes = sha1.ComputeHash (inputBytes); sha1.Clear (); output = BitConverter. toString (outputBytes); return output ;}

 

In this way, after we enter our server-side API address, we can smoothly complete the server configuration and bind the public account backend.

 

Summary

 

We have completed the first step in the development of public account configuration. In the future, we will use APIs to perform more operations, such as automatic reply to voice, articles, texts, and other messages.

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.