Example #1
0
        public static string GetToken(string secret)
        {
            TkDebug.AssertArgumentNullOrEmpty(secret, "secret", null);

            IRWLock         rwLock = BaseGlobalVariable.Current.CreateRWLock();
            CorpAccessToken token;

            using (rwLock)
            {
                if (fCurrent == null)
                {
                    token = RWLock.WriteLockAction(rwLock, () =>
                    {
                        CorpAccessToken result = CorpAccessToken.CreateToken(secret);
                        fCurrent = new CorpAccessTokenList();
                        fCurrent.AddToken(result);
                        SaveToken();
                        return(result);
                    });
                    return(token.Token);
                }
                else
                {
                    token = RWLock.ReadLockAction(rwLock, () =>
                    {
                        CorpAccessToken result = fCurrent.CorpTokens[secret];
                        if (result == null || result.IsExpire)
                        {
                            result = RWLock.WriteLockAction(rwLock, () =>
                            {
                                CorpAccessToken newToken = CorpAccessToken.CreateToken(secret);
                                fCurrent.CorpTokens.AddOrReplace(newToken);
                                SaveToken();
                                return(newToken);
                            });
                        }
                        return(result);
                    });
                    return(token.Token);
                }
            }
        }