【优化】优化微信公众号服务器配置远程校验可能存在校验失败的问题。

This commit is contained in:
jianweie code
2023-03-21 10:48:50 +08:00
parent 3e01e1c307
commit a44f9c8dd6

View File

@@ -84,18 +84,33 @@ namespace CoreCms.Net.Web.Controllers.WeChat
// 验证服务器推送 // 验证服务器推送
// 文档https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html // 文档https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
NLogUtil.WriteFileLog(LogLevel.Info, LogType.WeChat, "GET接收到微信推送的数据", JsonConvert.SerializeObject(new var getModel = new
{ {
timestamp, timestamp,
nonce, nonce,
signature, signature,
echoString echoString
})); };
var client = _weChatApiHttpClientFactory.CreateWeXinClient();
var valid = client.VerifyEventSignatureForEcho(callbackTimestamp: timestamp, callbackNonce: nonce, callbackSignature: signature);
return Content(!valid ? "fail" : echoString); //NLogUtil.WriteFileLog(LogLevel.Info, LogType.WeChat, "GET接收到微信推送的数据", JsonConvert.SerializeObject(getModel));
//var client = _weChatApiHttpClientFactory.CreateWeXinClient();
//var valid = client.VerifyEventSignatureForEcho(callbackTimestamp: timestamp, callbackNonce: nonce, callbackSignature: signature);
//return Content(!valid ? "fail" : echoString);
if (!CheckSignature.Check(signature, timestamp, nonce, _weChatOptions.WeiXinToken))
{
NLogUtil.WriteFileLog(LogLevel.Error, LogType.WeChat, "GET接收到微信推送的数据签名错误", JsonConvert.SerializeObject(getModel));
return Content("fail");
}
else
{
NLogUtil.WriteFileLog(LogLevel.Info, LogType.WeChat, "GET接收到微信推送的数据签名成功", JsonConvert.SerializeObject(getModel));
return Content(echoString);
}
} }
/// <summary> /// <summary>
@@ -271,7 +286,7 @@ namespace CoreCms.Net.Web.Controllers.WeChat
var userInfo = await _weChatUserInfoServices.QueryByClauseAsync(p => p.openid == eventModel.FromUserName); var userInfo = await _weChatUserInfoServices.QueryByClauseAsync(p => p.openid == eventModel.FromUserName);
if (userInfo == null) if (userInfo == null)
{ {
userInfo = new Model.Entities.CoreCmsUserWeChatInfo() userInfo = new Model.Entities.CoreCmsUserWeChatInfo()
{ {
isSubscribe = response.IsSubscribed, isSubscribe = response.IsSubscribed,