Example #1
0
        /// <summary>
        /// 刷新AuthorizerToken
        /// </summary>
        /// <param name="componentAccessToken"></param>
        /// <param name="componentAppId"></param>
        /// <param name="authorizerAppid"></param>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public static RefreshAuthorizerTokenResult RefreshAuthorizerToken(string componentAccessToken, string componentAppId, string authorizerAppid,
                                                                          string refreshToken)
        {
            var refreshResult = ComponentApi.ApiAuthorizerToken(componentAccessToken, componentAppId, authorizerAppid,
                                                                refreshToken);

            //更新到存储
            ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid, refreshResult);
            return(refreshResult);
        }
Example #2
0
        public IActionResult Refresh()
        {
            int timeSpanToUpdate = 300; //提前5分钟进行update
            var almostExpireList = db.AuthorizerTokens.Where(c => c.RefreshOn.AddSeconds(c.ExpiredIn - timeSpanToUpdate) > DateTime.Now);

            foreach (var token in almostExpireList)
            {
                var refreshToken = ComponentApi.ApiAuthorizerToken(ComponentKeys.GetInstance().AccessData.AccessCode, _wxConfig.AppId,
                                                                   token.AuthorizerAppId, token.AuthorizerRefreshToken);
                token.RefreshOn              = DateTime.Now;
                token.AuthorizerAccessToken  = refreshToken.authorizer_access_token;
                token.AuthorizerRefreshToken = refreshToken.authorizer_refresh_token;
                token.ExpiredIn              = refreshToken.expires_in;
            }

            db.SaveChanges();

            return(Content("Refresh Success"));
        }
Example #3
0
        private void RefreshMpAccessCode()
        {
            var    cts            = new ComponentTokenService();
            var    componentToken = cts.GetToken();
            string componentAppId = ConfigurationManager.AppSettings["AppId"];
            var    mpTokenList    = _context.MpTokens.ToList();

            foreach (var mpToken in mpTokenList)
            {
                if (DateTime.Now.AddSeconds(600) >= mpToken.RefreshOn.AddSeconds(mpToken.ExpiredIn))
                {
                    var refreshRlt = ComponentApi.ApiAuthorizerToken(componentToken.ComponentAccessToken, componentAppId,
                                                                     mpToken.MpAppId, mpToken.MpRefreshToken);
                    mpToken.ExpiredIn      = refreshRlt.expires_in;
                    mpToken.MpAccessToken  = refreshRlt.authorizer_access_token;
                    mpToken.MpRefreshToken = refreshRlt.authorizer_refresh_token;
                    mpToken.RefreshOn      = DateTime.Now;
                    _context.SaveChanges();
                }
            }
        }
Example #4
0
        public void RefreshAuthorizerTokenTest()
        {
            var componentAccessTokenResult = ComponentContainer.GetComponentAccessTokenResult(base._appId, base._ticket);

            Assert.IsNotNull(componentAccessTokenResult.component_access_token);

            var authorizerId   = "wxd7a61edcdce336b0";
            var authorizerInfo = ComponentApi.GetAuthorizerInfo(componentAccessTokenResult.component_access_token, base._appId, authorizerId);

            var authorizer_access_token  = authorizerInfo.authorization_info.authorizer_access_token;
            var authorizer_refresh_token = authorizerInfo.authorization_info.authorizer_refresh_token;

            Assert.IsNotNull(authorizerInfo.authorization_info.authorizer_access_token);
            Assert.IsNotNull(authorizerInfo.authorization_info.authorizer_refresh_token);

            Console.WriteLine("authorizer_access_token:" + authorizer_access_token);
            Console.WriteLine("authorizer_refresh_token:" + authorizer_refresh_token);

            var result = ComponentApi.ApiAuthorizerToken(componentAccessTokenResult.component_access_token,
                                                         base._appId, authorizerId, authorizerInfo.authorization_info.authorizer_refresh_token);

            Console.WriteLine("authorizer_access_token:" + result.authorizer_access_token);
            Console.WriteLine("authorizer_access_token:" + result.authorizer_refresh_token);
        }
        public static string ReloadToken(Guid CompanyID)
        {
            wx_userweixin wx = wx_userweixinService.instance().SingleByCompanyID(CompanyID);
            //if (wx.AppId == null || wx.AppSecret == null || wx.AppId.Trim().Length <= 0 || wx.AppSecret.Trim().Length <= 0)
            //{
            //    return "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!";
            //}
            var wx_open = wx_openInfoService.instance().Single(new Guid(System.Configuration.ConfigurationManager.AppSettings["openID"]));
            RefreshAuthorizerTokenResult result = ComponentApi.ApiAuthorizerToken(wx_open.open_access_token, wx_open.open_sAppID, wx.AppId, wx.refresh_token);

            if (result.errcode == Entity.Weixin.ReturnCode.请求成功)
            {
                wx.Access_Token  = result.authorizer_access_token;
                wx.refresh_token = result.authorizer_refresh_token;
                wx.expires_in    = result.expires_in;
                wx.ModifyTime    = DateTime.Now;
                if (wx_userweixinService.instance().Update(wx) == 1)
                {
                    return("ok");
                }
                return("更新数据库出错");
            }
            return(result.errcode.ToString());
        }