调式返回xml格式正确却提示“该公众号提供的服务出现故障”
t-jian 时间:2021-06-25
这里的配置已经是验证通过了
调试测试接口也是按照规定返回数据,但是还是提示”该公众号提供的服务出现故障,请稍后再试“的问题,各种度娘费了各种尝试。终于知道问题所在了。先看调式输出的内容 如下:
再看官方文档解释说明
对比一下终于发现 FromUserName (开发者微信号)?? ,晕死了,我这里竟然用错 开发者ID(AppID) ,大意了。更改这里再次调试终于好了。~~~
FromUserName 可以使用 **原始ID **或者 微信号
如图:
[ControllerName("WeChat")] [Route("/wechat")] public class WeChatController : AbpController { private readonly SignatureChecker _signatureChecker; private readonly IConfiguration _configuration; private readonly ILogger<WeChatController> _logger; public WeChatController(SignatureChecker signatureChecker, IConfiguration configuration, ILogger<WeChatController> logger) { _signatureChecker = signatureChecker; _configuration = configuration; _logger = logger; } //微信服务器验证 [HttpGet] [Route("service")] public async Task<string> Service(VerifyRequestDto input) { string token = _configuration["App:WeChat:ToKen"]; string str; if (_signatureChecker.Validate(token, input.Timestamp, input.Nonce, input.Signature)) str = input.EchoStr; else str = "非法参数"; return await Task.FromResult(str); } [HttpPost] [Route("service")] public async Task Service() { try { var query = HttpContext.Request.QueryString; _logger.LogInformation("接收到的参数 " + query.ToString()); using (var streamReader = new StreamReader(HttpContext.Request.Body, Encoding.UTF8)) { //string content = sr.ReadToEnd(); //.Net Core 3.0 默认不再支持同步读取 string content = streamReader.ReadToEndAsync().Result; } //这里可以从 HttpContext.Request.QueryString 中获取openid string msg = reText("你微信号", "你需要发送用户的 openid"); await HttpContext.Response.WriteAsync(msg); } catch (Exception ex) { _logger.LogException(ex); //记录异常日志 return; } } private string reText(string FromUserName, string openid) { StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", openid, FromUserName, DateTimeToUnixTimestamp(DateTime.Now))); resxml.AppendFormat("<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{0}]]></Content></xml>", "你好呀"); return resxml.ToString(); } public static long DateTimeToUnixTimestamp(DateTime dateTime) { var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind); return Convert.ToInt64((dateTime - start).TotalSeconds); } } public class SignatureChecker : ITransientDependency { /// <summary> /// 校验请求参数是否有效。 /// </summary> /// <param name="token"></param> /// <param name="timeStamp"></param> /// <param name="nonce"></param> /// <param name="signature"></param> /// <returns>返回 true 说明是有效请求,返回 false 说明是无效请求。</returns> public bool Validate(string token, string timeStamp, string nonce, string signature) { var paraArray = new[] { token, timeStamp, nonce }.OrderBy(x => x).ToArray(); var paraString = string.Join(string.Empty, paraArray); var bytes = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(paraString)); var signStrBuilder = new StringBuilder(); foreach (var @byte in bytes) { signStrBuilder.Append($"{@byte:x2}"); } return signStrBuilder.ToString().Equals(signature); } }
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。