Asp.net Core 微信公众号开发教程(4)——微信签名认证微信接入

 微信接入签名认证微信服务号开发

简介:Asp.net Core 微信公众号开发教程(4)——微信签名认证微信接入

    要开发微信公众号,获取公众号中用户、发送模版消息、自定义菜单等操作首先要进行微信签名认证。

下面我们来看一下微信签名认证的方法:

一.简单介绍官网接入逻辑

第一步:填写服务器配置

      登录微信公众平台官网后,在公众平台官网的开发-基本设置页面,勾选协议成为开发者,点击“修改配置”按钮,填写服务器地址(URL)、Token和EncodingAESKey,其中URL是开发者用来接收微信消息和事件的接口URL。Token可由开发者可以任意填写,用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)。EncodingAESKey由开发者手动填写或随机生成,将用作消息体加解密密钥。

同时,开发者可选择消息加解密方式:明文模式、兼容模式和安全模式。模式的选择与服务器配置在提交后都会立即生效,请开发者谨慎填写及选择。加解密方式的默认状态为明文模式,选择兼容模式和安全模式需要提前配置好相关加解密代码。

Asp.net Core 微信公众号开发教程(4)——微信签名认证微信接入_第1张图片

第二步:验证消息的确来自微信服务器

开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带参数如下表所示:

参数描述signature微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。timestamp时间戳nonce随机数echostr随机字符串

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。加密/校验流程如下:

1)将token、timestamp、nonce三个参数进行字典序排序 2)将三个参数字符串拼接成一个字符串进行sha1加密 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

第三步:依据接口文档实现业务逻辑

二.通过Asp.net Core 代码来演示具体操作

1.首先在appsettings.json文件中定义微信的相关常量信息

{

  // 日志处理

  "Logging": {

    "LogLevel": {

      "Default": "Information",

      "Microsoft": "Warning",

      "Microsoft.Hosting.Lifetime": "Information"

    },

    "LOG_LEVENL": "1"

  },

  "AllowedHosts": "*",

  // 数据库连接

  "ConnectionStrings": {

    "MySqlContext": "Server=localhost;port=3306;Database=tendb;user=root;password=root;SslMode=None;CharSet=utf8;"

  },

  // 微信相关配置

  "WeChatSetting": {

    "appid": "开发者ID(AppID)",

    "appsecret": "开发者密码(AppSecret)",

    // 后期自定义菜单用

    "menuUrl": "",

    // 后期发送模版消息用

    "TemplateId": ""

  }

}

2.写一个工具类Config.cs用来获取配置文件的参数

    public class Config

    {

        #region 根据Key取Value值

        ///

        /// 根据Key取Value值

        ///

        ///

        public static string GetValue(string key)

        {

            var configJson = GetJsonConfig();

            return configJson[key];

        }

        #endregion

        #region json配置文件读取

        ///

        /// json配置文件读取

        ///

        ///

        ///

        ///

        public static IConfigurationRoot GetJsonConfig(string configFileName = "appsettings.json", string basePath = "")

        {

            basePath = string.IsNullOrWhiteSpace(basePath) ? Directory.GetCurrentDirectory() : basePath;

            var builder = new ConfigurationBuilder().SetBasePath(basePath).AddJsonFile(configFileName);

            return builder.Build();

        }

        #endregion

    }

  3.定义一个验证签名的方法我是把所有的微信接口方法集成到一个方法里定义为baseApi.cs

    public class BasicApi

    {

        public BasicApi() { }

        #region 验证微信签名

        ///

        /// 验证微信签名

        ///

        /// AccessToken

        ///

        ///

        ///

        ///

        public static bool CheckSignature(string token, string signature, string timestamp, string nonce)

        {

            string[] ArrTmp = { token, timestamp, nonce };

            //字典排序

            Array.Sort(ArrTmp);

            string tmpStr = string.Join("", ArrTmp);

            //字符加密

            //tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");

            tmpStr = HmacSha1Sign(tmpStr);

            //tmpStr = tmpStr.ToLower();

            if (tmpStr == signature)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        #endregion

        }

4.新建一个API控制器类WeiChatController并添加一个get的方法

    [Route("api/[controller]")]

    public class WeiChatController : Controller

    {

        const string Token = "weixin";//定义一个局部变量不可以被修改,这里定义的变量要与接口配置信息中填写的Token一致

        #region 校验微信签名

        ///

        ///  校验微信签名

        ///

        /// 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

        /// 时间戳

        /// 随机数

        ///

        /// 随机字符串

        ///        

        [Route("WeChatCheck")]

        [HttpGet]

        public ActionResult WeChatCheck(string signature, string timestamp, string nonce, string echostr, string token)

        {

            if (BasicApi.CheckSignature(Token, signature, timestamp, nonce) && !string.IsNullOrEmpty(echostr))

            {

                return Content(echostr);

            }

            else

            {

                return null;

            }

        }

        #endregion

    }

5.方法添加完之后使用ngrok反射外网ip,然后在微信公众号的基本配置菜单中进行验证。验证之前需要配置IP白名单,这个是你服务器的ip。不过我个人开发用的时候一般用测试号来测试开发微信公众号,不需要配置,不过正式的微信服务号中需要配置ip白名单。

基本上验证微信接入签名是这样来实现的。

你可能感兴趣的:(Asp.net Core 微信公众号开发教程(4)——微信签名认证微信接入)