• WeiXin 验证成为开发者和更换服务器验证代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.XPath;using System.Security;
    using System.Net;
    using System.Collections;
    using System.Security.Cryptography;
    using System.Web.Security;
    
    public class Handler : IHttpHandler
    {
    
        const string Token = "token";  //你的token    
        public void ProcessRequest(HttpContext context)
        {
            //测试成为开发者,验证成功后注释掉。更换服务器需重新验证
           Valid();
    
        }
        /// <summary>
        /// 测试成为开发者
        /// </summary>
        private void Valid()
        {
    
            string echoStr = HttpContext.Current.Request.QueryString["echoStr"].ToString();
            if (CheckSignature())
            {
                if (!string.IsNullOrEmpty(echoStr))
                {
                    HttpContext.Current.Response.Write(echoStr);
                    HttpContext.Current.Response.End();
                }
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
      
    
        /// <summary>
        /// 验证微信签名
        /// </summary>
        /// * 将token、timestamp、nonce三个参数进行字典序排序
        /// * 将三个参数字符串拼接成一个字符串进行sha1加密
        /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
        /// <returns></returns>
        private bool CheckSignature()
        {
            string signature = HttpContext.Current.Request.QueryString["signature"].ToString();
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"].ToString();
            string nonce = HttpContext.Current.Request.QueryString["nonce"].ToString();
            string[] ArrTmp = { Token, timestamp, nonce };
            Array.Sort(ArrTmp);     //字典排序
            string tmpStr = string.Join("", ArrTmp);
            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        /// <summary>
        /// 处理微信服务器发送来的信息,进行处理并返回信息(微信信息回复)
        /// </summary>
        /// <param name="weixinXML"></param>
        private void ResponseMsg(string weixinXML)
        {
            //回复消息的部分:你的代码写在这里       
    
        }
    }
  • 相关阅读:
    Android学习第九天
    Android短信备份及插入笔记
    内容提供者实现应用访问另一个应用的数据库
    Verilog语言实现1/2分频
    QT中一个界面向另一个界面发送信号
    CMAKE设置Windows SDK编译版本
    VS2017下载地址
    VS 设置Windows SDK版本
    OBS 64bit版本编译
    Qt打包程序无法运行,提示应用程序无法正常启动0xc000007b解决办法
  • 原文地址:https://www.cnblogs.com/sekon/p/4685118.html
Copyright © 2020-2023  润新知