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

简介: 1:准备工作 进入微信公众号后台设置微信服务器配置参数(注意:Token和EncodingAESKey必须和微信服务器验证参数保持一致,不然验证不会通过)。 2:基本配置 设置为安全模式 3、代码实现(主要分为验证接口和消息处理接口): /// /// 验证接口 /// ...

1:准备工作

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

2:基本配置

设置为安全模式

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, 0, 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 == 0 && !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 == 0 && !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);
}
}

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

相关文章
|
11天前
|
小程序 开发者
uniapp实战 —— 开发微信小程序的调试技巧
uniapp实战 —— 开发微信小程序的调试技巧
14 1
|
11天前
|
小程序
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
18 0
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
|
11天前
|
存储 小程序 API
【微信小程序-原生开发+云开发+TDesign】修改用户头像(含wx.chooseMedia,wx.cloud.uploadFile,wx.cloud.deleteFile的使用)
【微信小程序-原生开发+云开发+TDesign】修改用户头像(含wx.chooseMedia,wx.cloud.uploadFile,wx.cloud.deleteFile的使用)
10 0
【微信小程序-原生开发+云开发+TDesign】修改用户头像(含wx.chooseMedia,wx.cloud.uploadFile,wx.cloud.deleteFile的使用)
|
10天前
|
小程序 定位技术 API
uniapp 开发微信小程序 --【地图】打开地图选择位置,打开地图显示位置(可开启导航)
uniapp 开发微信小程序 --【地图】打开地图选择位置,打开地图显示位置(可开启导航)
37 0
|
11天前
|
小程序 前端开发
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
20 0
|
11天前
|
小程序
【微信小程序-原生开发】客服
【微信小程序-原生开发】客服
81 0
|
11天前
|
小程序
【微信小程序-原生开发】wxml 支持 includes (wxml中执行函数的方法)
【微信小程序-原生开发】wxml 支持 includes (wxml中执行函数的方法)
23 0
|
11天前
|
小程序 JavaScript
【微信小程序-原生开发】watch 的实现
【微信小程序-原生开发】watch 的实现
19 0
|
11天前
|
小程序 JavaScript
【微信小程序-原生开发】实用教程11 - 用户登录鉴权(含云函数的创建、删除、使用,通过云函数获取用户的openid)
【微信小程序-原生开发】实用教程11 - 用户登录鉴权(含云函数的创建、删除、使用,通过云函数获取用户的openid)
17 0
|
11天前
|
缓存 小程序
【微信小程序-原生开发】启动时自动升级更新到最新版本
【微信小程序-原生开发】启动时自动升级更新到最新版本
13 0