我使用的是asp.net core 6.0 webapi想接入企业微信api接收消息,写入了添加日志已经获取到了signature,timestamp,nonce。并且Token也是从网页上复制下来的。域名也没有问题,用过这个域名接入过其他api但是我得到的签名和signature(企业微信的签名还是不一样)

这是通过行为日志获取到的数据,generatedSignature为我的签名,signature为企业微信的签名
[HttpGet("callback")]
public IActionResult VerifyCallback()
{
var requestUrl = Request.GetDisplayUrl();
string token = "3EaLwQCJdDx7n5wviXMd9VQyS".Trim('\r', '\n', '\t', ' ', ' ');
var signature = Request.Query["msg_signature"].ToString().Trim('\r', '\n', '\t', ' ', ' ');
var timestamp = Request.Query["timestamp"].ToString().Trim('\r', '\n', '\t', ' ', ' ');
var nonce = Request.Query["nonce"].ToString().Trim('\r', '\n', '\t', ' ', ' ');
_log_Service.Add_Action_Log(new Infrastructure.EFCore.Mysql.Actionlog()
{
Alid = Config.GUID2(),
AlapiUrl = "VerifyCallback",
AlcreateTime = DateTime.Now,
AlresultMessage = $"收到回调验证请求:signature={signature}, timestamp={timestamp}, nonce={nonce}",
Aldesc = requestUrl
});
var arr = new[] { token, timestamp, nonce };
Array.Sort(arr, StringComparer.Ordinal); // 纯ASCII码排序,和企业微信完全一致
string sortedStr = string.Join("", arr);
_log_Service.Add_Action_Log(new Infrastructure.EFCore.Mysql.Actionlog()
{
Alid = Config.GUID2(),
AlapiUrl = "VerifyCallback",
AlcreateTime = DateTime.Now,
AlresultMessage = $"排序后字符串: {sortedStr}"
});
using var sha1 = System.Security.Cryptography.SHA1.Create();
var utf8NoBom = new UTF8Encoding(false); // 无BOM,和企业微信编码规则对齐
byte[] hashBytes = sha1.ComputeHash(utf8NoBom.GetBytes(sortedStr));
StringBuilder generatedSignatureSb = new StringBuilder();
foreach (byte b in hashBytes)
{
generatedSignatureSb.Append(b.ToString("x2")); // 强制两位小写十六进制(如0a而非A)
}
string generatedSignature = generatedSignatureSb.ToString();
_log_Service.Add_Action_Log(new Infrastructure.EFCore.Mysql.Actionlog()
{
Alid = Config.GUID2(),
AlapiUrl = "VerifyCallback",
AlcreateTime = DateTime.Now,
AlresultMessage = $"generatedSignature:{generatedSignature}, signature:{signature}"
});
var isVerifySuccess = generatedSignature == signature;
if (isVerifySuccess)
{
_log_Service.Add_Action_Log(new Infrastructure.EFCore.Mysql.Actionlog()
{
Alid = Config.GUID2(),
AlapiUrl = "VerifyCallback",
AlcreateTime = DateTime.Now,
AlresultMessage = "回调验证通过",
Aldesc = $"generatedSignature:{generatedSignature}, signature:{signature}"
});
Response.ContentType = "text/plain; charset=UTF-8"; // 显式指定响应格式
return Content(Request.Query["echostr"].ToString().Trim('\r', '\n', '\t', ' ', ' '));
}
else
{
_log_Service.Add_Error(new Infrastructure.EFCore.Mysql.Errorlog()
{
Elapi = "VerifyCallback",
Elid = Config.GUID2(),
ElcreateTime = DateTime.Now,
Elmessage = "回调验证失败(签名不匹配)"
});
return BadRequest("签名不匹配");
}
}