Exemple #1
0
        /// <summary>
        /// 获取可用Token
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="getNewToken">是否强制重新获取新的Token</param>
        /// <returns></returns>
        public static AccessTokenResult GetTokenResult(string appId, bool getNewToken = false)
        {
            if (!AccessTokenCollection.ContainsKey(appId))
            {
                throw new WeixinException("此appId尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!");
            }

            var accessTokenBag = AccessTokenCollection[appId];

            if (getNewToken || accessTokenBag.ExpireTime <= DateTime.Now)
            {
                //已过期,重新获取
                accessTokenBag.AccessTokenResult = CommonApi.GetToken(accessTokenBag.AppId, accessTokenBag.AppSecret);
                accessTokenBag.ExpireTime        = DateTime.Now.AddSeconds(accessTokenBag.AccessTokenResult.expires_in);
            }
            return(accessTokenBag.AccessTokenResult);
        }
Exemple #2
0
        /// <summary>
        /// 获取可用Ticket
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="getNewTicket">是否强制重新获取新的Ticket</param>
        /// <returns></returns>
        public static JsApiTicketResult GetTicketResult(string appId, bool getNewTicket = false)
        {
            if (!JsApiTicketCollection.ContainsKey(appId))
            {
                throw new WeixinException("此appId尚未注册,请先使用JsApiTicketContainer.Register完成注册(全局执行一次即可)!");
            }

            var accessTicketBag = JsApiTicketCollection[appId];

            lock (accessTicketBag.Lock)
            {
                if (getNewTicket || accessTicketBag.ExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    accessTicketBag.JsApiTicketResult = CommonApi.GetTicket(accessTicketBag.AppId, accessTicketBag.AppSecret);
                    accessTicketBag.ExpireTime        = DateTime.Now.AddSeconds(accessTicketBag.JsApiTicketResult.expires_in);
                }
            }
            return(accessTicketBag.JsApiTicketResult);
        }
        /// <summary>
        /// 获取可用AccessTokenResult对象
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="getNewToken">是否强制重新获取新的Token</param>
        /// <returns></returns>
        public static AccessTokenResult GetAccessTokenResult(string appId, bool getNewToken = false)
        {
            if (!CheckRegistered(appId))
            {
                throw new UnRegisterAppIdException(appId, string.Format("此appId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!", appId));
            }

            var accessTokenBag = (AccessTokenBag)ItemCollection[appId];

            lock (accessTokenBag.Lock)
            {
                if (getNewToken || accessTokenBag.AccessTokenExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    accessTokenBag.AccessTokenResult     = CommonApi.GetToken(accessTokenBag.AppId, accessTokenBag.AppSecret);
                    accessTokenBag.AccessTokenExpireTime = DateTime.Now.AddSeconds(accessTokenBag.AccessTokenResult.expires_in);
                }
            }
            return(accessTokenBag.AccessTokenResult);
        }
        /// <summary>
        /// 获取可用Ticket
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="getNewTicket">是否强制重新获取新的Ticket</param>
        /// <returns></returns>
        public static JsApiTicketResult GetJsApiTicketResult(string appId, bool getNewTicket = false)
        {
            if (!CheckRegistered(appId))
            {
                throw new UnRegisterAppIdException(null, "此appId尚未注册,请先使用JsApiTicketContainer.Register完成注册(全局执行一次即可)!");
            }

            var jsApiTicketBag = (JsApiTicketBag)ItemCollection[appId];

            lock (jsApiTicketBag.Lock)
            {
                if (getNewTicket || jsApiTicketBag.JsApiTicketExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    jsApiTicketBag.JsApiTicketResult     = CommonApi.GetTicket(jsApiTicketBag.AppId, jsApiTicketBag.AppSecret);
                    jsApiTicketBag.JsApiTicketExpireTime = DateTime.Now.AddSeconds(jsApiTicketBag.JsApiTicketResult.expires_in);
                }
            }
            return(jsApiTicketBag.JsApiTicketResult);
        }
        /// <summary>
        /// 获取可用Token
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="componentVerifyTicket"></param>
        /// <param name="getNewToken">是否强制重新获取新的Token</param>
        /// <returns></returns>
        public static PreAuthCodeResult GetTokenResult(string componentAppId, string componentVerifyTicket, bool getNewToken = false)
        {
            if (!PreAuthCodeCollection.ContainsKey(componentAppId))
            {
                throw new WeixinException("此appId尚未注册,请先使用PreAuthCodeContainer.Register完成注册(全局执行一次即可)!");
            }

            var accessTokenBag = PreAuthCodeCollection[componentAppId];

            lock (accessTokenBag.Lock)
            {
                if (getNewToken || accessTokenBag.ExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    accessTokenBag.PreAuthCodeResult = CommonApi.GetPreAuthCode(accessTokenBag.ComponentAppId, accessTokenBag.ComponentAppSecret, componentVerifyTicket);
                    accessTokenBag.ExpireTime        = DateTime.Now.AddSeconds(accessTokenBag.PreAuthCodeResult.expires_in);
                }
            }
            return(accessTokenBag.PreAuthCodeResult);
        }