当前位置:网站首页>用.Net Core接入微信公众号开发
用.Net Core接入微信公众号开发
2022-07-01 12:34:00 【dotNET跨平台】
Part1前言
最近想写一点基于.Net Core微信公众号开发的文章
Part2测试公众号申请
测试公众号申请地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

微信公众号开发文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
通过微信扫一扫授权就能进入到测试号管理页面。

测试账号拥有几乎所有的公众号接口,而个人只能申请订阅号,几乎没有接口可用,并且消息推送还会受到次数限制。如果只是做开发测试的话,那么测试帐号比较好用
进行接口配置信息
需要一台服务器进行到微信授权回调到线上的域名。但是对于没有服务器,或者初次接触微信公众号开发的同学们就有点不太友好,不方便本地调试。
于是就尝试通过内网穿透的方法调试本地代码,常见的工具有ngrok,FastTunnel,花生壳,natapp等等
Part3开发
新建.Net Core webapi项目,将本地服务映射到外网
笔者在这里使用的是natapp


验证消息的确来自微信服务器

我们可根据微信开发文档轻松的写出 微信后台的“接口配置信息”,一共有四个参数
[HttpGet]
[ActionName("")]
public string Get(string signature, string timestamp, string nonce, string echostr)
{
}有洁癖的同学可能觉得有四个参数,再在controller层一个一个地接收就会显得特别的笨重了。那我们也可以使用实体类来接受参数
[HttpGet]
[ActionName("")]
public string Get([FromUri] VxModel vxModel)
{
}[FromUri]属性处理查询参数,即"?"之后的键值对在URI中。
加密/校验
流程如下:
将token、timestamp、nonce三个参数进行字典序排序 2)将三个参数字符串拼接成一个字符串进行sha1加密 3)开发者获得加密后的字符串可与 signature 对比,标识该请求来源于微信.若确认此次 GET 请求来自微信服务器,请原样返回 echostr 参数内容。
将微信公众号设置好的token,定义成一个常量,用来校验
public const string Token = "weixin";然后根据流程进行签名校验
public static bool Check(string signature, string timestamp, string nonce)
{
string[] value = new string[3]
{
WeixinSetting.Token,
timestamp,
nonce
}.OrderBy(o=>o).ToArray();
string s = string.Join("", value);
byte[] array = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(s));
StringBuilder stringBuilder = new StringBuilder();
byte[] array2 = array;
foreach (byte b in array2)
{
stringBuilder.AppendFormat("{0:x2}", b);
}
return signature==stringBuilder.ToString();
}完整接口如下:
[HttpGet]
[ActionName("")]
public string Get(string signature, string timestamp, string nonce, string echostr)
{
if (CheckSignatureHelper.Check(signature, timestamp, nonce))
{
return echostr;
}
else
{
return "校验失败";
}
}
这样就校验成功了。
边栏推荐
- 被锡膏坑了一把
- 硬阈值(Hard Thresholding)函数解读[通俗易懂]
- ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
- 【datawhale202206】pyTorch推荐系统:精排模型 DeepFM&DIN
- Share several tools for designing exquisite circuit diagrams
- Sort out relevant contents of ansible
- CPI tutorial - asynchronous interface creation and use
- 木架的场景功能
- Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
- [Suanli network] technological innovation of Suanli Network -- key technology of operation service
猜你喜欢
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter

【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾

Sleep quality today 79 points
![[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE](/img/8f/64fea641730795a2b5252cc2c8cdd2.png)
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE

Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
![[106] 360 check font - check whether the copyright of local Fonts is commercially available](/img/a7/615e8000647b56f03a6a1d3dd81b6d.jpg)
[106] 360 check font - check whether the copyright of local Fonts is commercially available

第十四章 信号(四)- 多进程任务示例

Ansible相关内容梳理

Indefinite integral
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration
随机推荐
I wish you all a happy reunion
The operation process of using sugar to make a large data visualization screen
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
One year anniversary of bitbear live studio, hero rally order! I invite you to take a group photo!
2022-06-28-06-29
使用nvm管理nodejs(把高版本降级为低版本)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
系统测试UI测试总结与问题(面试)
Common chart usage of Bi tools
Friends day 2022
JS related interview questions and answers (1)
Ansible Playbook
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
手把手教你完成图像分类实战——基于卷积神经网络的图像识别
Relationship between accuracy factor (DOP) and covariance in GPS data (reference link)
Sleep quality today 79 points
CPI tutorial - asynchronous interface creation and use
Virtualenv+pipenv virtual environment management
华为面试题: 招聘