GetPreAuthCode() 공개 정적인 메소드

获取预授权码
public static GetPreAuthCode ( string componentAppId, string componentAccessToken, int timeOut = Config.TIME_OUT ) : PreAuthCodeResult
componentAppId string 第三方平台方appid
componentAccessToken string
timeOut int
리턴 Senparc.Weixin.Open.Entities.PreAuthCodeResult
예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <summary>
        /// 获取可用Token
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="getNewToken">是否强制重新获取新的Token</param>
        /// <returns></returns>
        public static PreAuthCodeResult GetPreAuthCodeResult(string componentAppId, bool getNewToken = false)
        {
            if (!CheckRegistered(componentAppId))
            {
                throw new WeixinOpenException(UN_REGISTER_ALERT);
            }

            var componentBag = ItemCollection[componentAppId];

            lock (componentBag.Lock)
            {
                if (getNewToken || componentBag.PreAuthCodeExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    var componentVerifyTicket = TryGetComponentVerifyTicket(componentAppId);

                    var accessToken = TryGetComponentAccessToken(componentAppId, componentBag.ComponentAppSecret, componentVerifyTicket);

                    var preAuthCodeResult = CommonApi.GetPreAuthCode(componentBag.ComponentAppId, accessToken);

                    //if (preAuthCodeResult)
                    //{

                    //}

                    componentBag.PreAuthCodeResult = preAuthCodeResult;

                    //TODO:这里有出现expires_in=0的情况,导致始终处于过期状态(也可能是因为参数过期等原因没有返回正确的数据,待观察)
                    var expiresIn = componentBag.PreAuthCodeResult.expires_in > 0
                        ? componentBag.PreAuthCodeResult.expires_in
                        : 60 * 20;//默认为20分钟
                    componentBag.PreAuthCodeExpireTime = DateTime.Now.AddSeconds(expiresIn);
                }
            }
            return(componentBag.PreAuthCodeResult);
        }