zoukankan      html  css  js  c++  java
  • 微信公众平台开发(二)

    一、实现URL接入

    相关参数描述

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    
    namespace WeiXinWebApplication
    {
        public class CheckSignature
        {
            /// <summary>
            /// 网站没有提供Token情况下的默认Token
            /// </summary>
            public const string Token = "weixin";
    
            /// <summary>
            /// 检查签名是否正确
            /// </summary>
            /// <param name="signature">微信加密签名</param>
            /// <param name="timestamp">时间戳</param>
            /// <param name="nonce">随机数</param>
            /// <param name="token"></param>
            /// <returns></returns>
            public static bool Check(string signature, string timestamp, string nonce, string token)
            {
                return signature == GetSignature(timestamp, nonce, token);
            }
    
            /// <summary>
            /// 返回正确的签名
            /// </summary>
            /// <param name="timestamp"></param>
            /// <param name="nonce"></param>
            /// <param name="token"></param>
            /// <returns></returns>
            public static string GetSignature(string timestamp, string nonce, string token = Token)
            {
                token = token ?? Token;
                string[] arr = new[] {token,timestamp,nonce }.OrderBy(z=>z).ToArray();
                string arrString = string.Join("",arr);
                System.Security.Cryptography.SHA1 shal = System.Security.Cryptography.SHA1.Create();
                byte[] shalArr = shal.ComputeHash(Encoding.UTF8.GetBytes(arrString));
                StringBuilder enText = new StringBuilder();
                foreach (var b in shalArr)
                {
                    enText.AppendFormat("{0:x2}",b);
                }
                return enText.ToString();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WeiXinWebApplication
    {
        public partial class Index : System.Web.UI.Page
        {
            /// <summary>
            /// 与微信公众账号后台的Token设置保持一致,区分大小写
            /// </summary>
            private readonly string Token = "weixin";
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Auth();
            }
    
            /// <summary>
            /// 处理微信服务器验证消息
            /// </summary>
            private void Auth()
            {
                string signature = Request["signature"];
                string timestamp = Request["timestamp"];
                string nonce = Request["nonce"];
                string echostr = Request["echostr"];
                if (Request.HttpMethod == "GET")
                {
                    if (CheckSignature.Check(signature, timestamp, nonce, Token))
                    {
                        WriteContent(echostr);
                    }
                    else
                    {
                        WriteContent("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, Token) + "." + "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众号后台的URL,请注意保持Token一致。");
                    }
                    Response.End();
                }
            }
    
            private void WriteContent(string str)
            {
                Response.Output.Write(str);
            }
        }
    }

     二、接入微信公众平台

    这样我们就成功实现了URL接入微信公众平台的工作。

  • 相关阅读:
    Unix UTC时间转化为本地时间的一个MFC实现
    不规则按钮Button修正版
    不规则按钮,支持普通Button,Radio Button, Check Button
    Kimi ga Suki da to Sakebitai
    将adb for visual studio 从26升级29版本
    直到世界的尽头
    Your Song
    Win32创建后台进程
    全金属外壳的歌词
    C#获取本机IP(排除IPV6,仅获取IPV4)的方法转载
  • 原文地址:https://www.cnblogs.com/zhanghaomars/p/4950687.html
Copyright © 2011-2022 走看看