/// <summary> /// 用于解密从微信服务器中,发送过来的加密信息 /// </summary> /// <param name="data"></param> /// <returns></returns> public ResultReturn <string> DecryptMessage(string appId, string encryptMsg) { var config = _gateway.Get <MPConfiguration>(appId); var ret = WXBizMsgCrypt.DecryptMsg(encryptMsg, config.EncryptAESKey); if (ret.code == 0) { return(new SuccessResultReturn <string>(ret.msg)); } else { return(new FailResultReturn <string>("解密失败", ret.code)); } }
/// <summary> /// 通过网页跳转后获取到的code,取得用户openID /// </summary> /// <param name="appID"></param> /// <param name="code">授权跳转后,传入的code值</param> /// <returns></returns> public async Task <ResultReturn <GetAccessToken_Result> > GetAccessToken(string appID, string code) { var item = _gateway.Get(appID); if (item == null) { throw new ArgumentOutOfRangeException(nameof(appID)); } if (string.IsNullOrWhiteSpace(code)) { throw new ArgumentNullException(nameof(code)); } var result = await CommonApi.Get(appID, $"/sns/oauth2/access_token?appid={appID}&secret={item.AppSerect}&code={code}&grant_type=authorization_code"); if (result.IsSuccess) { var json = result.ReturnData; var ret = new GetAccessToken_Result() { AccessToken = json.GetString("access_token"), Expires = json.GetInt("expires_in"), RefreshToken = json.GetString("refresh_token"), OpenId = json.GetString("openid"), Type = json.GetString("scope") == "base" ? SnsapiType.Base : SnsapiType.UserInfo, UionId = json.GetString("unionid") }; return(new SuccessResultReturn <GetAccessToken_Result>(ret)); } else { return(result.Cast <GetAccessToken_Result>(default));
public async Task <IActionResult> Service([FromServices] IWechatGateway gateway, [FromQuery] string signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromQuery] string echostr, [FromRoute] string appID = "", [FromServices] ILoggerFactory logger = null ) { if (gateway == null) { return(Content("请先注册微信小程序")); } logger?.CreateLogger("miniprogram")?.Log(LogLevel.Trace, $"微信调用:signature={signature},timestamp={timestamp},nonce={nonce},echostr={echostr}"); if (string.IsNullOrWhiteSpace(appID)) { return(Content("AppID不能为空")); } var config = gateway.Get <MiniProgramConfiguration>(appID); if (config == null) { return(Content("该AppID非小程序配置")); } if (CheckSignature.Check(signature, timestamp, nonce, config.Token)) { return(Content(echostr)); //返回随机字符串则表示验证通过 } else { return(Content($"failed:{signature},{CheckSignature.GetSignature(timestamp, nonce, config.Token).ToString()}。如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。")); } }
public async Task <IActionResult> ServicePost([FromServices] IWechatGateway gateway, [FromQuery] string signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromQuery] string echostr, [FromRoute] string appID = "", [FromServices] ILoggerFactory logger = null, [FromServices] IWechatMPApi mpApi = null, [FromServices] MessageQueue msgHandler = null, [FromServices] IMPMessageExecutor messageExecutor = null ) { //return Content(""); if (gateway == null) { return(Content("请先注册微信公众号服务")); } logger?.CreateLogger("weixin")?.Log(LogLevel.Trace, $"微信调用:signature={signature},timestamp={timestamp},nonce={nonce},echostr={echostr}"); if (string.IsNullOrWhiteSpace(appID)) { return(Content("AppID不能为空")); } var config = gateway.Get(appID) as MPConfiguration; if (config == null) { return(Content("该AppID非公众号配置或AppID不存在")); } if (CheckSignature.Check(signature, timestamp, nonce, config.Token)) { return(Content("校验无效,请检查token")); } if (messageExecutor == null) { return(Content("success")); } Request.EnableBuffering(); //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var inputStream = Request.Body; inputStream.Position = 0; var xml = inputStream.ReadToEnd(); var msg = mpApi.Message.DecodeMPRequestMsg(xml); if (msg.IsSuccess) { if (await msgHandler.AddMessage(msg.ReturnData)) { var response = await messageExecutor.Execute(msg.ReturnData); if (response == null) { return(Content("success")); } //if (!string.IsNullOrWhiteSpace(config.EncryptAESKey)) //{ // return Content(mpApi.Message.EncryptMessage(appID, response.ToXml())); //} return(Content(response.ToXml())); } else { return(Content("success")); } } else { if (msg.ReturnCode == 1000) { return(Content("success")); } return(Content("error")); } }