首页 > 学院 > 开发设计 > 正文

微信公众平台开发(二)

2019-11-14 13:47:05
字体:
来源:转载
供稿:网友

一、实现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接入微信公众平台的工作。

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表