zoukankan      html  css  js  c++  java
  • 微信测试号使用笔记:接口配置信息验证

    一、前提条件,要有一台服务器

    请填写接口配置信息,此信息需要你有自己的服务器资源,填写的URL需要正确响应微信发送的Token验证,请阅读消息接口使用指南

    二、C#代码示例

    但是上面链接中的代码是PHP示例代码,因此我整理了一份C#版的,亲测能用。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WXtoken
    {
        public partial class index : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //验证token
                string poststring = string.Empty;
                string token = "aabbcc";   //验证token,随意填写  
                if (string.IsNullOrEmpty(token))
                {
                    return;
                }
                string echostr = HttpContext.Current.Request.QueryString["echostr"];
                string signature = HttpContext.Current.Request.QueryString["signature"];
                string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
                string nonce = HttpContext.Current.Request.QueryString["nonce"];
                if (checkSignature(token, signature, timestamp, nonce, nonce))
                {
                    HttpContext.Current.Response.Write(echostr);
                    HttpContext.Current.Response.End();
                }
            }
            #region 微信接口对接验证代码
            public bool checkSignature(string token, string signature, string timestamp, string nonce, string echostr)
            {
                List<string> list = new List<string>();
                list.Add(token);
                list.Add(timestamp);
                list.Add(nonce);
                list.Sort();
    
                string res = string.Join("", list.ToArray());
                Byte[] dataToHash = Encoding.ASCII.GetBytes(res);
                byte[] hashvalue = ((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(dataToHash);
                StringBuilder sb = new StringBuilder();
                foreach (byte b in hashvalue)
                {
                    sb.Append(b.ToString("x2"));
                }
    
                if (signature == sb.ToString())
                    return true;
                else
                    return false;
            }
            #endregion
        }
    }
    

    三、配置失败原因整理:

    • 端口错误,必须是80或者443端口
    • 参数错误,建议从官方文档复制参数到项目中(我就是有两个字母写反了,尴尬)
    • 地址或者Token错误

    四、文章下载:

    点击下载MarkDown压缩包

  • 相关阅读:
    比较实用的断点调试技巧
    objc非主流代码技巧
    0代码ViewController
    xib的动态桥接
    ios中集合遍历方法的比较和技巧
    再见了NSLog
    Reactive Cocoa Tutorial [4] = 只取所需的Filters
    objc@interface的设计哲学与设计技巧
    ARC下dealloc过程及.cxx_destruct的探究
    Reactive Cocoa Tutorial [3] = "RACSignal的巧克力工厂“;
  • 原文地址:https://www.cnblogs.com/kudsu/p/13877554.html
Copyright © 2011-2022 走看看