public async Task <ResultDto> JsSdkAsync(string url) { var configSection = _configuration.GetSection("WeChat:Web"); //获取时间戳 var timestamp = JSSDKHelper.GetTimestamp(); //获取随机码 var nonceStr = JSSDKHelper.GetNoncestr(); var appId = configSection.GetValue <string>("AppId"); var appSecret = configSection.GetValue <string>("AppSecret"); //获取票证 var jsTicket = await JsApiTicketContainer.TryGetJsApiTicketAsync(appId, appSecret); //获取签名 //nonceStr = "Wm3WZYTPz0wzccnW"; //jsTicket = "sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg"; //timestamp = "1414587457"; //url = "http://mp.weixin.qq.com?params=value"; //url = url?? Request.GetAbsoluteUri(); var signature = JSSDKHelper.GetSignature(jsTicket, nonceStr, timestamp, url); var resultDto = new ResultDto { Result = true, Data = new { appId, timestamp, nonceStr, signature }, Message = "查询成功" }; _logger.LogDebug(Serialize.ToJson(new { url, appId, jsTicket, timestamp, nonceStr, signature })); return(resultDto); }
public async Task <(bool Success, string Ticket, string ErrorMessage)> GetJsApiTicketAsync(string mpAppId, string mpAppSecret) { var success = false; var errorMessage = string.Empty; string ticket = null; try { ticket = await JsApiTicketContainer.TryGetJsApiTicketAsync(mpAppId, mpAppSecret); success = true; } catch (ErrorJsonResultException ex) { if (ex.JsonResult.errcode == ReturnCode.调用接口的IP地址不在白名单中) { var startIndex = ex.JsonResult.errmsg.IndexOf("invalid ip ", StringComparison.Ordinal) + 11; var endIndex = ex.JsonResult.errmsg.IndexOf(" ipv6", StringComparison.Ordinal); var ip = ex.JsonResult.errmsg.Substring(startIndex, endIndex - startIndex); errorMessage = $"调用接口的IP地址不在白名单中,请进入微信公众平台,将本服务器的IP地址 {ip} 添加至白名单"; } else { errorMessage = $"API 调用发生错误:{ex.JsonResult.errmsg}"; } } catch (Exception ex) { errorMessage = $"执行过程发生错误:{ex.Message}"; } return(success, ticket, errorMessage); }
public static async Task <WeixinResponse> WeixinSignResponse(this string url) { var response = new WeixinResponse(); var timestamp = JSSDKHelper.GetTimestamp(); var noncestr = JSSDKHelper.GetNoncestr(); var ticket = await JsApiTicketContainer.TryGetJsApiTicketAsync(AppId, AppSecret); if (ticket.IsNullOrEmpty()) { response.Message = "获取ticket出错了~~"; } var signature = JSSDKHelper.GetSignature(ticket, noncestr, timestamp, url); if (signature.IsNullOrEmpty()) { response.Message = "获取signature出错了~~"; } response.Timestamp = timestamp; response.Noncestr = noncestr; response.Ticket = ticket; response.Signature = signature; return(response); }
/// <summary> /// 【异步方法】获取给UI使用的JSSDK信息包 /// </summary> /// <param name="appId"></param> /// <param name="appSecret"></param> /// <param name="url"></param> /// <returns></returns> public static async Task <JsSdkUiPackage> GetJsSdkUiPackageAsync(string appId, string appSecret, string url) { //获取时间戳 var timestamp = GetTimestamp(); //获取随机码 string nonceStr = GetNoncestr(); string ticket = await JsApiTicketContainer.TryGetJsApiTicketAsync(appId, appSecret); //获取签名 string signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, url); //返回信息包 return(new JsSdkUiPackage(appId, timestamp, nonceStr, signature)); }