首页 技术 正文
技术 2022年11月23日
0 收藏 715 点赞 4,461 浏览 2118 个字

1:准备工作

进入微信公众号后台设置微信服务器配置参数(注意:Token和EncodingAESKey必须和微信服务器验证参数保持一致,不然验证不会通过)。

微信公众号开发–.Net Core实现微信消息加解密

2:基本配置

设置为安全模式

微信公众号开发–.Net Core实现微信消息加解密

3、代码实现(主要分为验证接口和消息处理接口):

/// <summary>
/// 验证接口
/// </summary>
/// <param name="signature">签名</param>
/// <param name="timestamp">时间戳</param>
/// <param name="nonce"></param>
/// <param name="echostr"></param>
/// <returns></returns>
[HttpGet, Route("Message")]
[AllowAnonymous]
public ActionResult MessageGet(string signature, string timestamp, string nonce, string echostr)
{
if (new SecurityHelper().CheckSignature(signature, timestamp, nonce, _settings.Value.Token))
{
return Content(echostr);
}
return Content("");
}/// <summary>
/// 接收消息并处理和返回相应结果
/// </summary>
/// <param name="msg_signature">当加密模式时才会有该变量(消息签名)</param>
/// <param name="signature">签名</param>
/// <param name="timestamp">时间戳</param>
/// <param name="nonce"></param>
/// <returns></returns>
[HttpPost, Route("Message")]
[AllowAnonymous]
public ActionResult MessagePost(string msg_signature, string signature, string timestamp, string nonce)
{
try
{
if (!new SecurityHelper().CheckSignature(signature, timestamp, nonce, _settings.Value.Token))
{
return Content(null);
}
using (Stream stream = HttpContext.Request.Body)
{
byte[] buffer = new byte[HttpContext.Request.ContentLength.Value];
stream.Read(buffer, , buffer.Length);
string content = Encoding.UTF8.GetString(buffer);
if (!string.IsNullOrWhiteSpace(msg_signature)) // 消息加密模式
{
string decryptMsg = string.Empty;
var wxBizMsgCrypt = new WXBizMsgCrypt(_settings.Value.Token, _settings.Value.EncodingAESKey, _settings.Value.AppId);
int decryptResult = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, content, ref decryptMsg);
if (decryptResult == && !string.IsNullOrWhiteSpace(decryptMsg))
{
string resultMsg = new WechatMessageHelper().MessageResult(decryptMsg);
string sEncryptMsg = string.Empty;
if (!string.IsNullOrWhiteSpace(resultMsg))
{
int encryptResult = wxBizMsgCrypt.EncryptMsg(resultMsg, timestamp, nonce, ref sEncryptMsg);
if (encryptResult == && !string.IsNullOrWhiteSpace(sEncryptMsg))
{
return Content(sEncryptMsg);
}
}
}
}
else // 消息未加密码处理
{
string resultMsg = new WechatMessageHelper().MessageResult(content);
return Content(resultMsg);
}
return Content(null);
}
}
catch (Exception ex)
{
_logger.LogError("接收消息并处理和返回相应结果异常:", ex);
return Content(null);
}
}

加解密实现(微信公众号官网有源码)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,466
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,095
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765