Getting a record of C # developed a micro letter

Reprinted connection:  https://www.cnblogs.com/zhankui/p/4515905.html

 

Before always see some examples of micro-channel development, but as a beginner there will be a lot of questions before I can find a lot of posts, but ultimately failed to solve, and now the hands of just a project, summarize the share going to do but can not move the hand of my friends, this is only my personal experience as a Talking (college students), inadequacies also hope you have educated us!

  Before the development we need three things, the next will be introduced one by one to prepare three things:

  First, we need to apply for a public platform account;

  Second, you need a domain name space, that have a space on the network;

  Again, we need a website;

  A: micro-channel public platform account , how to register online have a very detailed tutorial, please see the venue ;

  Then the question is, please review carefully the following figure (for the convenience of explanation, the picture treated):

Here we must talked about the URL and token, according to the official date of the event, here recommend Wan network and Ali cloud launch two years of free cloud hosting and virtual domain, yes, yes that is what we need, and I not in advertising, after all, the party of students, compared to government constraints, can not try to use the free, pulling away, and activities address: http: //www.net.cn/hosting/free/, you can follow it a very simple, not to say, however, be noted that: the free version does not provide telephone service, as since the opening of 60 consecutive days, not for the record, and to resolve binding domain, the virtual machine will be withdrawn and deleted . This compares pit father, after all, there is no free lunch, or buy a domain name it, there is a little more than 20 (the cheapest);

He went to the key areas of:

The above is my own to buy the domain name, the following you can see, Free Web Hosting gift, but can also be used;

Well, ready to work winding down, the thing now is to code;

First of all:

When the developer first submitted verification application, the micro-channel server sends a GET request to fill the URL, and take four parameters (signature, timestamp, nonce, echostr), the developer through the efficacy of the signature (i.e., signature), and to determining the authenticity of the message of the entry.

Thereafter, every time the developer receiving user messages, when micro-channel will also bring the first three parameters (signature, timestamp, nonce) URL, the developer still access developer provided by this provision determination efficacy signature authenticity of the message . Efficacy way for the first time to submit application for verification agreement.

parameter description
signature Micro-channel encrypted signature, signature combines a timestamp parameter developer filled token request parameters, nonce parameters.
timestamp Timestamp
nonce random number
echostr Random string

Developer test signature is performed by the request check (check mode below). If confirmed the GET request to the server from the micro-letter, please return it unchanged echostr content parameters, the access into effect, become a successful developer, or access failure.

Encryption / verification process is as follows: 
1. The token, timestamp, nonce three parameters lexicographical sort 
2. The three splice into a parameter string string sha1 encrypted 
string 3. The developer may obtain an encrypted signature comparison, the identifier of the request from the micro-channel 

so that:

Copy the code

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


public class Handler : IHttpHandler
{
    //private static string msg;
    private static string xmlMsg;
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        if (context.Request.HttpMethod.ToLower().Equals("get")) 
            // capacity, the access into effect, become a successful developer, or access failure.
            // If the confirmation letter from the micro-server GET request, please return it unchanged within echostr parameters
        {
            //开发者通过检验signature对请求进行校验(下面有校验方式)。
            //context.Response.Write(msg); 
            ValidateUrl (); 
            the URL // check for micro-channel interfaces 
        } 
        the else 
        { 
            // receive and respond 
            HandleMsg (); 
        } 
    } 

    Private void HandleMsg () 
    { 
        // receiving 
        / * 
         * ToUserName developer micro signal 
            FromUserName sender account (a the OpenID) 
            CreateTime message creation time (integer) 
            the MsgType text 
            the content text message contents 
            MsgId message id, 64-bit integer 
        * /
        = The HttpContext.Current context the HttpContext; 
        // packet received XML 
        Stream XmlStream = context.Request.InputStream; 
        // configured xml objects 
        the XmlDocument the XmlDocument new new DOC = (); 
        doc.Load (XmlStream); 
        the XmlElement the rootElement = doc.DocumentElement; / / Get root 

        // parse XML data 
        String = toUserName rootElement.SelectSingleNode ( "ToUserName") the InnerText;. 
        String fromusername = rootElement.SelectSingleNode ( "fromusername") the InnerText;. 
        . msgType String = rootElement.SelectSingleNode ( "the msgType") the InnerText ; 
        // this is a text message content to distinguish, for the text 
        String content = rootElement.SelectSingleNode ( "the content") the InnerText.;
        //msg = string.Format("{0}-{1}-{2}-{3}", toUserName, fromUserName, msgType, content);//测试
        //响应
        /* 
         * <XML> 
             <ToUserName> <[CDATA [TOUSER]]!> </ ToUserName>
             <FromUserName><![CDATA[fromUser]]></FromUserName> 
             <CreateTime>1348831860</CreateTime>
             <MsgType><![CDATA[text]]></MsgType>
             <Content><![CDATA[this is a test]]></Content>
             <MsgId>1234567890123456</MsgId>
             </xml>
         */

        if (content.Contains("BD") || content.Contains("bd"))
        //content.Contains返回一个值,该值指示指定的system.whether an object exists in this string string 
            string name = sArray [2];
            string [] = sArray content.Split ( '+');
        { 
            string STR = "your information : "+" name: "+ name +"学号:" + no;
            //SelectStudentInfo();
            
                       + "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>"
                       + "<CreateTime>" + GetCreateTime() + "</CreateTime>"


        

    
    
        /*
            random number nonce 
            echostr random string 
         * / 
        the HttpContext context = the HttpContext.Current; 
        // context object Get 
        String context.Request Signature = [ "Signature"]; 
        String context.Request timestamp = [ "timestamp"]; 
        String = nonce context. the Request [ "the nonce"]; 
        String echostr context.Request = [ "echostr"]; 

        String token = "123456"; 
        // custom token 
        // encryption / verification process is as follows: 

        String [] = {temp1 token, timestamp, } the nonce; 
        .. 1 // the token, timestamp, nonce three parameters lexicographically sorted 
        Array. 
        // 2 splicing three parameters sha1 string into a string encrypted. 
        string = temp2 of String.Join ( "", temp1) ; 
        String = FormsAuthentication.HashPasswordForStoringInConfigFile Temp3 (temp2 of, "SHAl"); 

        // developer. 3 string obtained can be compared with an encrypted signature, logo. the request from the micro-channel 
        // SHA1 case there is difference, and then comparing the first to lower case 
        IF (temp3.ToLower () Equals (Signature).) 
        { 
            context.Response.Write (echostr); 
            // if the same micro-channel server returns required signature, it is not necessary to handle different 
        } 
    } 
    


    public BOOL the IsReusable 
    { 
        GET 
        { 
            return to false; 
        } 
    } 

}

Copy the code

Mainly to see the developer documentation ;

Well, the rest is posted to the server just fine.

Released six original articles · won praise 189 · views 280 000 +

Guess you like

Origin blog.csdn.net/newbie_xymt/article/details/104561335