.netcore2.1 JS-SDK 从后台获取微信签名,实现自定义分享标题、描述、图片

  最近项目移动端需要实现微信自定义分享功能,包含分享自定义标题、描述等。

  • 首先到公众号的后台,功能设置里面,添加将要被分享的域名,如图

 

.netcore2.1  JS-SDK  从后台获取微信签名,实现自定义分享标题、描述、图片

 

  jsapi_ticket

  生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket 。

  生成签名步骤,获取AccessToken-》获取JsApiTicket-》生成签名

  appsettings配置文件添加配置

 "Api": {
    "JSJDKBaseApi": "https://api.weixin.qq.com/cgi-bin"
  },
  "JSJDK": {
    "AppId": "***",
    "Secret": "****"
  }
  • 创建签名公用类
 public class JSSDKSignHelper
    {
        private static AccessTokenResponse singleAccessToken;
        
        private  string _tencentApi { get; set; }
        private  string _appid { get; set; }
        private  string _appSecret { get; set; }

        public  JSSDKSignHelper(IConfiguration config)
        {
           
            _tencentApi = config["Api:JSJDKBaseApi"]; 
            _appid = config["JSJDK:AppId"];
            _appSecret = config["JSJDK:Secret"];
        }

        public string GetAccessTokenSingle(out bool isNewObj)
        {
            isNewObj = false;
            if (singleAccessToken != null && (singleAccessToken.expire_out > DateTime.Now.AddHours(2)))
            {
                return singleAccessToken.access_token;
            }
            else
            {
                var response = GetAccessToken();
                if (response.errcode == 0)
                {
                    response.expire_out = DateTime.Now.AddHours(expireHour);
                    singleAccessToken = response;
                    isNewObj = true;
                    return response?.access_token;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取access_token
        /// </summary>
        /// <returns></returns>
        private AccessTokenResponse GetAccessToken()
        {
           
            string url = _tencentApi + "/token?grant_type=client_credential&appid=" + _appid + "&secret=" + _appSecret;
            var response = ApiClient.GetJson<AccessTokenResponse>(url);
            return response;
        }

        /// <summary>
        /// 获取JsApiTicket
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public  string GetJsApiTicket(string accessToken)
        {
            if (string.IsNullOrWhiteSpace(accessToken))
            {
                return null;
            }
            string url = _tencentApi + $"/ticket/getticket?type=jsapi&access_token={accessToken}";

            var response = ApiClient.GetJson<AccessTicketResponse>(url);

            return response?.ticket;
        }

        /// <summary>
        /// 获取签名
        /// </summary>
        /// <param name="jsapi_ticket"></param>
        /// <param name="noncestr">随机字符串(必须与wx.config中的nonceStr相同)</param>
        /// <param name="timestamp">时间戳(必须与wx.config中的timestamp相同)</param>
        /// <param name="url">当前网页的URL,不包含#及其后面部分(必须是调用JS接口页面的完整URL)</param>
        /// <returns></returns>
        public string  GetSignature(string jsapi_ticket, string noncestr, string timestamp, string url)
        {
            if (string.IsNullOrEmpty(jsapi_ticket) || string.IsNullOrEmpty(noncestr) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(url))
                return null;
            var string1Builder = new StringBuilder();
            string1Builder.Append("jsapi_ticket=").Append(jsapi_ticket).Append("&")
                          .Append("noncestr=").Append(noncestr).Append("&")
                          .Append("timestamp=").Append(timestamp).Append("&")
                          .Append("url=").Append(url.IndexOf("#") >= 0 ? url.Substring(0, url.IndexOf("#")) : url);
             return Sha1Sign(string1Builder.ToString());
        }
       
        /// <summary>
        /// Sha1加密签名
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string Sha1Sign(string str)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = System.Text.UTF8Encoding.Default.GetBytes(str);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string signature = BitConverter.ToString(bytes_sha1_out);
            signature = signature.Replace("-", "").ToLower();
            return signature;
        }

    }
  • 签名定义的Model
public class AccessTokenResponse: BaseResponse
    {
        /// <summary>
        /// 返回access_token值,有效期7200秒
        /// </summary>
        public string access_token { get; set; }
        /// <summary>
        /// 过期时间,单位小时
        /// </summary>
        public DateTime expire_out { get; set; }


    }

    public class AccessTicketResponse : BaseResponse
    {
        /// <summary>
        /// 返回Ticket票据,有效期7200秒
        /// </summary>
        public string ticket { get; set; }

    }
  public class BaseResponse
    {
        /// <summary>
        /// 过期时间,单位秒
        /// </summary>
        public string expire_in { get; set; }
        /// <summary>
        /// 错误代码
        /// </summary>
        public Int32 errcode { get; set; }
        /// <summary>
        /// 错误信息描述
        /// </summary>
        public string errmsg { get; set; }
    }
  • 创建生成签名的时间戳和随机串
 /// <summary>
        /// 获取微信JS-JDK时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);

            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
     
        /// <summary>
        /// JS-JDK 创建随机字符串
        /// </summary>
        /// <returns></returns>
        public static string CreatenNonce_str()
        {
            Random r = new Random();
            var sb = new StringBuilder();
            var length = strs.Length;
            for (int i = 0; i < 15; i++)
            {
                sb.Append(strs[r.Next(length - 1)]);
            }
            return sb.ToString();
        }
  • Action中实际调用
        [HttpPost]
        [Route("GetSignature")]
        public string GetSignature(string url= "http://www.sina.cn/")
        {
            try
            {
                if (string.IsNullOrEmpty(url)) return "url不能为空";
                string _jsTicket = null, _signature = null;
                string _accessToken = _jsSignHelper.GetAccessTokenSingle(out bool isNewObj);
                WxConfigModel model = new WxConfigModel();
                model.AppId = appId;
                model.TimeStamp = MyUtil.GetTimeStamp();
                model.NonceStr = MyUtil.CreatenNonce_str();
                if (isNewObj|| _wxConfigModel.Signature==null)
                {

                    _jsTicket = _jsSignHelper.GetJsApiTicket(_accessToken);
                    _signature = _jsSignHelper.GetSignature(_jsTicket, model.NonceStr, model.TimeStamp, url);
                    model.jsTicket = _jsTicket;
                    model.Signature = _signature;
                    _wxConfigModel = model;
                }
                return JsonConvert.SerializeObject(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }    

  注意:

  1. 签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。
  2. 移动端分享时,不能本地测试,需要发布到外网测试,否则报签名错误
    1. .netcore2.1  JS-SDK  从后台获取微信签名,实现自定义分享标题、描述、图片
  3. 生成的签名,可以实用校验工具校验是否正确。https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign

.netcore2.1 JS-SDK 从后台获取微信签名,实现自定义分享标题、描述、图片

上一篇:微信推送功能


下一篇:微信小程序开发之简单的授权登录