예제 #1
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
        /// </summary>
        private static void Register(string componentAppId, string authorizerAppId)
        {
            var componentBag = ComponentContainer.TryGetItem(componentAppId);

            if (componentBag == null)
            {
                throw new WeixinOpenException(string.Format("注册AuthorizerContainer之前,必须先注册对应的ComponentContainer!ComponentAppId:{0},AuthorizerAppId:{1}", componentAppId, authorizerAppId));
            }

            using (FlushCache.CreateInstance())
            {
                Update(authorizerAppId, new AuthorizerBag()
                {
                    AuthorizerAppId = authorizerAppId,
                    ComponentAppId  = componentAppId,

                    AuthorizationInfo           = new AuthorizationInfo(),
                    AuthorizationInfoExpireTime = DateTime.MinValue,

                    AuthorizerInfo = new AuthorizerInfo(),
                    //AuthorizerInfoExpireTime = DateTime.MinValue,

                    JsApiTicketResult     = new JsApiTicketResult(),
                    JsApiTicketExpireTime = DateTime.MinValue,
                });
            }

            //TODO:这里也可以考虑尝试进行授权(会影响速度)
        }
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token
        /// </summary>
        /// <param name="appId">微信公众号后台的【开发】>【基本配置】中的“AppID(应用ID)”</param>
        /// <param name="appSecret">微信公众号后台的【开发】>【基本配置】中的“AppSecret(应用密钥)”</param>
        /// <param name="name">标记AccessToken名称(如微信公众号名称),帮助管理员识别</param>
        public static void Register(string appId, string appSecret, string name = null)
        {
            //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
            RegisterFunc = () =>
            {
                using (FlushCache.CreateInstance())
                {
                    var bag = new AccessTokenBag()
                    {
                        //Key = appId,
                        Name                  = name,
                        AppId                 = appId,
                        AppSecret             = appSecret,
                        AccessTokenExpireTime = DateTime.MinValue,
                        AccessTokenResult     = new AccessTokenResult()
                    };
                    Update(appId, bag);
                    return(bag);
                }
            };
            RegisterFunc();

            //为JsApiTicketContainer进行自动注册
            JsApiTicketContainer.Register(appId, appSecret, name);

            //OAuthAccessTokenContainer进行自动注册
            OAuthAccessTokenContainer.Register(appId, appSecret, name);
        }
예제 #3
0
        public static void Register(string corpId, string corpSecret, string name = null)
        {
            //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
            RegisterFunc = () =>
            {
                using (FlushCache.CreateInstance())
                {
                    var bag = new AccessTokenBag()
                    {
                        Name              = name,
                        CorpId            = corpId,
                        CorpSecret        = corpSecret,
                        ExpireTime        = DateTime.MinValue,
                        AccessTokenResult = new AccessTokenResult()
                    };
                    Update(BuildingKey(corpId, corpSecret), bag);
                    return(bag);
                }
            };
            RegisterFunc();

            JsApiTicketContainer.Register(corpId, corpSecret);   //连带注册JsApiTicketContainer

            ProviderTokenContainer.Register(corpId, corpSecret); //连带注册ProviderTokenContainer
        }
예제 #4
0
        /// <summary>
        /// 注册应用凭证信息,此操作为注册完整信息
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="clientSecret"></param>
        /// <param name="accessToken"></param>
        /// <param name="expiresIn"></param>
        /// <param name="expiryTime"></param>
        /// <param name="refreshToken"></param>
        /// <param name="redirectUri"></param>
        /// <param name="appName"></param>
        public static void Register(string clientId, string clientSecret, string accessToken, int expiresIn, int expiryTime, string refreshToken, string redirectUri, string appName = null)
        {
            if (!CheckRegistered(clientId))
            {
                RegisterFunc = () =>
                {
                    using (FlushCache.CreateInstance())
                    {
                        var bag = new AccessTokenBag()
                        {
                            Key                   = clientId,
                            AppName               = appName,
                            ClientId              = clientId,
                            ClientSecret          = clientSecret,
                            RedirectUri           = redirectUri,
                            AccessTokenExpireTime = ApiUtility.GetExpireTime(expiresIn),
                            AccessTokenResult     = new AccessTokenResult()
                        };

                        bag.AccessTokenResult.data.access_token  = accessToken;
                        bag.AccessTokenResult.data.refresh_token = refreshToken;
                        bag.AccessTokenResult.data.expires_in    = expiresIn;
                        bag.AccessTokenResult.data.expiry_time   = expiryTime;

                        Update(clientId, bag);
                        return(bag);
                    }
                };

                RegisterFunc();
            }
        }
예제 #5
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="clientSecret"></param>
        /// <param name="name"></param>
        public static void Register(string clientId, string clientSecret, string preAuthCode, string redirectUri, string name = null)
        {
            //如果此clinetId 未注册,则进行注册
            if (!CheckRegistered(clientId))
            {
                //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
                RegisterFunc = () =>
                {
                    using (FlushCache.CreateInstance())
                    {
                        var bag = new AccessTokenBag()
                        {
                            Key                   = clientId,
                            Name                  = name,
                            ClientId              = clientId,
                            ClientSecret          = clientSecret,
                            PreAuthCode           = preAuthCode,
                            RedirectUri           = redirectUri,
                            AccessTokenExpireTime = DateTime.MinValue,
                            AccessTokenResult     = new AccessTokenResult()
                        };
                        Update(clientId, bag);
                        return(bag);
                    }
                };

                RegisterFunc();

                //OAuthAccessTokenContainer进行自动注册
                //OAuthAccessTokenContainer.Register(clientId, clientSecret, name);
            }
        }
예제 #6
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token,
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="componentAppSecret"></param>
        /// <param name="getComponentVerifyTicketFunc">获取ComponentVerifyTicket的方法</param>
        /// <param name="getAuthorizerRefreshTokenFunc">从数据库中获取已存的AuthorizerAccessToken的方法</param>
        /// <param name="authorizerTokenRefreshedFunc">AuthorizerAccessToken更新后的回调</param>
        /// <param name="name">标记Authorizer名称(如微信公众号名称),帮助管理员识别</param>
        public static void Register(string componentAppId, string componentAppSecret, Func <string, string> getComponentVerifyTicketFunc, Func <string, string, string> getAuthorizerRefreshTokenFunc, Action <string, string, RefreshAuthorizerTokenResult> authorizerTokenRefreshedFunc, string name = null)
        {
            //激活消息队列线程

            if (GetComponentVerifyTicketFunc == null)
            {
                GetComponentVerifyTicketFunc  = getComponentVerifyTicketFunc;
                GetAuthorizerRefreshTokenFunc = getAuthorizerRefreshTokenFunc;
                AuthorizerTokenRefreshedFunc  = authorizerTokenRefreshedFunc;
            }

            RegisterFunc = () =>
            {
                using (FlushCache.CreateInstance())
                {
                    var bag = new ComponentBag()
                    {
                        Name               = name,
                        ComponentAppId     = componentAppId,
                        ComponentAppSecret = componentAppSecret,
                    };
                    Update(componentAppId, bag);
                    return(bag);
                }
            };
            RegisterFunc();
        }
예제 #7
0
        /// <summary>
        /// 【异步方法】尝试更新AuthorizationInfo(如果没有AccessToken则不更新)。
        /// 如果AuthorizerBag更新则返回最新的对象,否则返回null
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="authorizerAppid"></param>
        /// <param name="authorizerAccessToken"></param>
        /// <param name="authorizerRefreshToken"></param>
        /// <param name="expiresIn"></param>
        public static async Task TryUpdateAuthorizationInfoAsync(string componentAppId, string authorizerAppid, string authorizerAccessToken, string authorizerRefreshToken, int expiresIn)
        {
            await TryRegisterAsync(componentAppId, authorizerAppid).ConfigureAwait(false);

            if (expiresIn > 0 && authorizerAccessToken != null)
            {
                using (FlushCache.CreateInstance())
                {
                    var authorizerBag = await TryGetItemAsync(authorizerAppid).ConfigureAwait(false);

                    var refreshTokenChanged = authorizerBag == null ||
                                              authorizerBag.AuthorizationInfo.authorizer_access_token != authorizerAccessToken ||
                                              authorizerBag.AuthorizationInfo.authorizer_refresh_token != authorizerRefreshToken;

                    authorizerBag = authorizerBag ?? new AuthorizerBag();

                    authorizerBag.AuthorizationInfo.authorizer_access_token  = authorizerAccessToken;
                    authorizerBag.AuthorizationInfo.authorizer_refresh_token = authorizerRefreshToken;
                    authorizerBag.AuthorizationInfo.expires_in = expiresIn;
                    authorizerBag.AuthorizationInfoExpireTime  = ApiUtility.GetExpireTime(expiresIn);

                    await UpdateAsync(authorizerBag, null).ConfigureAwait(false);//立即更新

                    //通知变更
                    if (refreshTokenChanged)
                    {
                        ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid,
                                                                        new RefreshAuthorizerTokenResult(authorizerAccessToken, authorizerRefreshToken, expiresIn));
                    }
                }
            }
        }
        /// <summary>
        /// 尝试更新AuthorizationInfo(如果没有AccessToken则不更新)。
        /// 如果AuthorizerBag更新则返回最新的对象,否则返回null
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="authorizerAppid"></param>
        /// <param name="authorizerAccessToken"></param>
        /// <param name="authorizerRefreshToken"></param>
        /// <param name="expiresIn"></param>
        public static void TryUpdateAuthorizationInfo(string componentAppId, string authorizerAppid, string authorizerAccessToken, string authorizerRefreshToken, int expiresIn)
        {
            TryRegister(componentAppId, authorizerAppid);

            if (expiresIn > 0 && authorizerAccessToken != null)
            {
                using (FlushCache.CreateInstance())
                {
                    var authorizerBag = TryGetItem(authorizerAppid);

                    var refreshTokenChanged = authorizerBag.AuthorizationInfo.authorizer_access_token !=
                                              authorizerAccessToken ||
                                              authorizerBag.AuthorizationInfo.authorizer_refresh_token !=
                                              authorizerRefreshToken;

                    authorizerBag.AuthorizationInfo.authorizer_access_token  = authorizerAccessToken;
                    authorizerBag.AuthorizationInfo.authorizer_refresh_token = authorizerRefreshToken;
                    authorizerBag.AuthorizationInfo.expires_in = expiresIn;
                    authorizerBag.AuthorizationInfoExpireTime  = ApiUtility.GetExpireTime(expiresIn);

                    Update(authorizerBag, null);//立即更新

                    //通知变更
                    if (refreshTokenChanged)
                    {
                        ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid,
                                                                        new RefreshAuthorizerTokenResult(authorizerAccessToken, authorizerRefreshToken, expiresIn));
                    }
                }
            }
        }
예제 #9
0
 public static void Register(string appId, string appSecret)
 {
     using (FlushCache.CreateInstance())//注释掉这一行,FlushCacheTest()将失败
     {
         Update(appId, new CacheTestContainerBag()
         {
             AppId     = appId,
             AppSecret = appSecret
         });
     }
 }
예제 #10
0
 /// <summary>
 /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token,
 /// </summary>
 /// <param name="corpId"></param>
 /// <param name="corpSecret"></param>
 public static void Register(string corpId, string corpSecret)
 {
     using (FlushCache.CreateInstance())
     {
         Update(corpId, new ProviderTokenBag()
         {
             CorpId              = corpId,
             CorpSecret          = corpSecret,
             ExpireTime          = DateTime.MinValue,
             ProviderTokenResult = new ProviderTokenResult()
         });
     }
 }
예제 #11
0
 /// <summary>
 /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
 /// </summary>
 /// <param name="appId"></param>
 /// <param name="appSecret"></param>
 public static void Register(string appId, string appSecret)
 {
     using (FlushCache.CreateInstance())
     {
         Update(appId, new JsApiTicketBag()
         {
             AppId                 = appId,
             AppSecret             = appSecret,
             JsApiTicketExpireTime = DateTime.MinValue,
             JsApiTicketResult     = new JsApiTicketResult()
         });
     }
 }
        //static Dictionary<string, JsApiTicketBag> JsApiTicketCollection =
        //   new Dictionary<string, JsApiTicketBag>(StringComparer.OrdinalIgnoreCase);

        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <param name="name">标记JsApiTicket名称(如微信公众号名称),帮助管理员识别</param>
        public static void Register(string appId, string appSecret, string name = null)
        {
            using (FlushCache.CreateInstance())
            {
                Update(appId, new OAuthAccessTokenBag()
                {
                    Name      = name,
                    AppId     = appId,
                    AppSecret = appSecret,
                    OAuthAccessTokenExpireTime = DateTime.MinValue,
                    OAuthAccessTokenResult     = new OAuthAccessTokenResult()
                });
            }
        }
예제 #13
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token。
        /// 执行此注册过程,会连带注册ProviderTokenContainer。
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="corpSecret"></param>
        public static void Register(string corpId, string corpSecret)
        {
            using (FlushCache.CreateInstance())
            {
                Update(corpId, new AccessTokenBag()
                {
                    CorpId            = corpId,
                    CorpSecret        = corpSecret,
                    ExpireTime        = DateTime.MinValue,
                    AccessTokenResult = new AccessTokenResult()
                });
            }

            ProviderTokenContainer.Register(corpId, corpSecret);//连带注册ProviderTokenContainer
        }
예제 #14
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token
        /// </summary>
        /// <param name="appId">微信公众号后台的【开发】>【基本配置】中的“AppID(应用ID)”</param>
        /// <param name="appSecret">微信公众号后台的【开发】>【基本配置】中的“AppSecret(应用密钥)”</param>
        public static void Register(string appId, string appSecret)
        {
            using (FlushCache.CreateInstance())
            {
                Update(appId, new AccessTokenBag()
                {
                    AppId                 = appId,
                    AppSecret             = appSecret,
                    AccessTokenExpireTime = DateTime.MinValue,
                    AccessTokenResult     = new AccessTokenResult()
                });
            }

            //为JsApiTicketContainer进行自动注册
            JsApiTicketContainer.Register(appId, appSecret);
        }
예제 #15
0
        /// <summary>
        /// 更新或插入
        /// </summary>
        /// <param name="key">如果留空,则新建一条记录</param>
        /// <param name="values">值</param>
        /// <param name="appObj">对象</param>
        /// <returns></returns>
        public static AppBag UpdateAppBag(string key, string values, string appObj)
        {
            key = key ?? SessionHelper.GetNewThirdSessionName();

            using (FlushCache.CreateInstance())
            {
                var appBag = new AppBag()
                {
                    Key        = key,
                    Values     = values,
                    AppObj     = appObj,
                    ExpireTime = GetExpireTime()
                };
                Update(key, appBag);
                return(appBag);
            }
        }
예제 #16
0
        /// <summary>
        /// 更新或插入SessionBag
        /// </summary>
        /// <param name="key">如果留空,则新建一条记录</param>
        /// <param name="openId">OpenId</param>
        /// <param name="sessionKey">SessionKey</param>
        /// <returns></returns>
        public static SessionBag UpdateSession(string key, string openId, string sessionKey)
        {
            key = key ?? SessionHelper.GetNewThirdSessionName();

            using (FlushCache.CreateInstance())
            {
                var sessionBag = new SessionBag()
                {
                    Key        = key,
                    OpenId     = openId,
                    SessionKey = sessionKey,
                    ExpireTime = GetExpireTime()
                };
                Update(key, sessionBag);
                return(sessionBag);
            }
        }
예제 #17
0
        //static Dictionary<string, JsApiTicketBag> JsApiTicketCollection =
        //   new Dictionary<string, JsApiTicketBag>(StringComparer.OrdinalIgnoreCase);

        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <param name="name">标记JsApiTicket名称(如微信公众号名称),帮助管理员识别</param>
        /// 此接口不提供异步方法
        public static void Register(string appId, string appSecret, string name = null)
        {
            RegisterFunc = () =>
            {
                using (FlushCache.CreateInstance())
                {
                    var bag = new OAuthAccessTokenBag()
                    {
                        Name      = name,
                        AppId     = appId,
                        AppSecret = appSecret,
                        OAuthAccessTokenExpireTime = DateTime.MinValue,
                        OAuthAccessTokenResult     = new OAuthAccessTokenResult()
                    };
                    Update(appId, bag);
                    return(bag);
                }
            };
            RegisterFunc();
        }
예제 #18
0
 public static void Register(string corpId, string corpSecret, string name = null)
 {
     RegisterFunc = () =>
     {
         using (FlushCache.CreateInstance())
         {
             var bag = new ProviderTokenBag()
             {
                 Name                = name,
                 CorpId              = corpId,
                 CorpSecret          = corpSecret,
                 ExpireTime          = DateTime.MinValue,
                 ProviderTokenResult = new ProviderTokenResult()
             };
             Update(BuildingKey(corpId, corpSecret), bag);
             return(bag);
         }
     };
     RegisterFunc();
 }
예제 #19
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token,
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <param name="componentAppSecret"></param>
        /// <param name="getComponentVerifyTicketFunc">获取ComponentVerifyTicket的方法</param>
        /// <param name="getAuthorizerRefreshTokenFunc">从数据库中获取已存的AuthorizerAccessToken的方法</param>
        /// <param name="authorizerTokenRefreshedFunc">AuthorizerAccessToken更新后的回调</param>
        public static void Register(string componentAppId, string componentAppSecret, Func <string, string> getComponentVerifyTicketFunc, Func <string, string> getAuthorizerRefreshTokenFunc, Action <string, RefreshAuthorizerTokenResult> authorizerTokenRefreshedFunc)
        {
            //激活消息列队线程


            if (GetComponentVerifyTicketFunc == null)
            {
                GetComponentVerifyTicketFunc  = getComponentVerifyTicketFunc;
                GetAuthorizerRefreshTokenFunc = getAuthorizerRefreshTokenFunc;
                AuthorizerTokenRefreshedFunc  = authorizerTokenRefreshedFunc;
            }

            using (FlushCache.CreateInstance())
            {
                Update(componentAppId, new ComponentBag()
                {
                    ComponentAppId     = componentAppId,
                    ComponentAppSecret = componentAppSecret,
                });
            }
        }
예제 #20
0
 public static void Register(string appId, string appSecret, string name = null)
 {
     //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
     RegisterFunc = () =>
     {
         using (FlushCache.CreateInstance())
         {
             var bag = new JsApiTicketBag()
             {
                 Name              = name,
                 AppId             = appId,
                 AppSecret         = appSecret,
                 ExpireTime        = DateTime.MinValue,
                 JsApiTicketResult = new JsApiTicketResult()
             };
             Update(BuildingKey(appId, appSecret), bag);
             return(bag);
         }
     };
     RegisterFunc();
 }
예제 #21
0
        /// <summary>
        /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="clientSecret"></param>
        /// <param name="name">标记Authorizer名称,帮助管理员识别</param>
        private static void Register(string clientId, string clientSecret, string name = null)
        {
            RegisterFunc = () =>
            {
                using (FlushCache.CreateInstance())
                {
                    var bag = new AuthrizationBag()
                    {
                        Name = name,

                        AuthorizerClientId     = clientId,
                        AuthorizerClientSecret = clientSecret,
                        OAuthAccessTokenResult = new OAuthAccessTokenResult(),
                        AccessTokenExpiryTime  = DateTime.MinValue,
                    };
                    Update(clientId, bag);
                    return(bag);
                }
            };
            RegisterFunc();

            //TODO:这里也可以考虑尝试进行授权(会影响速度)
        }
예제 #22
0
        /// <summary>
        /// 获取Session
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static SessionBag GetSession(string key)
        {
            var bag = TryGetItem(key);

            if (bag == null)
            {
                return(null);
            }

            if (bag.ExpireTime < DateTime.Now)
            {
                //已经过期
                Cache.RemoveFromCache(key);
                return(null);
            }

            using (FlushCache.CreateInstance())
            {
                bag.ExpireTime = GetExpireTime();//滚动过期时间
                Update(key, bag);
            }
            return(bag);
        }
예제 #23
0
        public void CreateInstanceTest()
        {
            var instance = FlushCache.CreateInstance();

            Assert.IsNotNull(instance);
        }