zoukankan      html  css  js  c++  java
  • 微信开发-验证服务器

    微信自己开发的第一步要验证自己的服务器,只需写一个空网页,接收微信服务器发过来的字符串,然后验证签名后将字符串原样返回,微信服务器收到这个字符串后即可验证成功。


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Diagnostics;
    
    namespace Weixin
    {
        public partial class Index : System.Web.UI.Page
        {
            public const String TOKEN = "zhuoteng123"; 
            protected void Page_Load(object sender, EventArgs e)
            {
                String echoStr = Request["echostr"];
    
                Debug.Write("soupld:"
                    + DateTime.Now.ToString("HH-mm-ss")
                    + "load page");
    
                if (this.checkSignature())
                {
                    Response.Write(echoStr);
                }
            }
    
            //验证
            private bool checkSignature()
            {
                string signature = Request["signature"];
                string timestamp = Request["timestamp"];
                string nonce = Request["nonce"];
    
                string token = TOKEN;
                string[] tmpArr = new string[] { token, timestamp, nonce };
                Array.Sort(tmpArr);
                string tmpStr = string.Join("", tmpArr);
                //sha1加密
                System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                byte[] secArr = sha1.ComputeHash(System.Text.Encoding.Default.GetBytes(tmpStr));
                tmpStr = BitConverter.ToString(secArr).Replace("-", "").ToLower();
    
                Debug.Write("soupld:" 
                    + DateTime.Now.ToString("HH-mm-ss") 
                    + ":signature=" + signature 
                    + ";timestamp=" + timestamp 
                    + ";nonce=" + nonce 
                    + ";");
                
                if (tmpStr == signature)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }


    写好的网页放到服务器上,用IIS配置好网站,注意要把将此页面设置为默认页面,让网站启动后即可接受微信服务器消息。

    版权声明:

  • 相关阅读:
    MySQL常用函数
    MDK关于microlib库问题笔记
    STM32F407ADC多通道+定时器触发+DMA模式设置
    12864LCD学习笔记
    转载:有趣的uC/OS-View
    怎样下载专利文件(特别是中英文对照的专利文件)
    FPGA学习之按键去抖
    数据采样与处理算法
    FTU几种保护逻辑研究
    2016第一篇之AD7606调试
  • 原文地址:https://www.cnblogs.com/walccott/p/4957089.html
Copyright © 2011-2022 走看看