private TokenProvider.TokenInfo GetTokenInfoFromCache(TokenProvider.Key key) { TokenProvider.TokenInfo tokenInfo; TokenProvider.TokenInfo tokenInfo1; lock (this.mutex) { if (!this.CacheTokens) { tokenInfo1 = new TokenProvider.TokenInfo(); } else if (this.tokenCache.TryGetValue(key, out tokenInfo)) { DateTime utcNow = DateTime.UtcNow; if (tokenInfo.WebTokenCacheUntil < utcNow) { tokenInfo.ResetWebToken(); } if (tokenInfo.SecurityTokenCacheUntil < utcNow) { tokenInfo.ResetSecurityToken(); } tokenInfo1 = tokenInfo; } else { tokenInfo = new TokenProvider.TokenInfo(); this.tokenCache.Add(key, tokenInfo); tokenInfo1 = tokenInfo; } } return(tokenInfo1); }
private void OnCompletion(IAsyncResult result) { DateTime dateTime; this.OnEndTokenProviderCallback(result, out dateTime); if (dateTime >= DateTime.UtcNow) { lock (this.TokenProvider.mutex) { TokenProvider.TokenInfo tokenInfoFromCache = this.TokenProvider.GetTokenInfoFromCache(this.cacheKey); this.OnUpdateTokenProviderCacheEntry(dateTime, ref tokenInfoFromCache); } } }
protected override void OnUpdateTokenProviderCacheEntry(DateTime cacheUntil, ref TokenProvider.TokenInfo tokenInfo) { if (tokenInfo.WebToken == null || cacheUntil > tokenInfo.WebTokenCacheUntil) { tokenInfo.WebToken = this.WebToken; tokenInfo.WebTokenCacheUntil = cacheUntil; } }
protected override bool OnProcessCachedEntryFromTokenProvider(TokenProvider.TokenInfo tokenInfo) { this.WebToken = tokenInfo.WebToken; return(this.WebToken != null); }
protected abstract void OnUpdateTokenProviderCacheEntry(DateTime cacheUntil, ref TokenProvider.TokenInfo tokenInfo);
protected abstract bool OnProcessCachedEntryFromTokenProvider(TokenProvider.TokenInfo tokenInfo);
protected override IEnumerator <IteratorAsyncResult <T> .AsyncStep> GetAsyncSteps() { bool flag; TimeSpan timeSpan; while (true) { bool flag1 = false; if (!this.bypassCache) { lock (this.TokenProvider.mutex) { TokenProvider.TokenInfo tokenInfoFromCache = this.TokenProvider.GetTokenInfoFromCache(this.cacheKey); flag = this.OnProcessCachedEntryFromTokenProvider(tokenInfoFromCache); } if (flag) { break; } } Stopwatch stopwatch = Stopwatch.StartNew(); try { TokenProvider.GetTokenAsyncResultBase <T> getTokenAsyncResultBase = this; IteratorAsyncResult <T> .BeginCall getTokenBeginCall = this.GetTokenBeginCall; yield return(getTokenAsyncResultBase.CallAsync(getTokenBeginCall, (T thisPtr, IAsyncResult r) => thisPtr.OnCompletion(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException != null) { goto Label0; } stopwatch.Stop(); MessagingPerformanceCounters.IncrementTokenAcquisitionLatency(this.appliesToUri, stopwatch.ElapsedTicks); MessagingPerformanceCounters.IncrementTokensAcquiredPerSec(this.appliesToUri, 1); break; } finally { stopwatch.Stop(); } Label0: MessagingPerformanceCounters.IncrementTokenAcquisitionFailuresPerSec(this.appliesToUri, 1); SecurityTokenException lastAsyncStepException = base.LastAsyncStepException as SecurityTokenException; TokenProviderException tokenProviderException = base.LastAsyncStepException as TokenProviderException; TimeoutException timeoutException = base.LastAsyncStepException as TimeoutException; if (timeoutException != null && timeoutException.InnerException != null && timeoutException.InnerException is WebException) { flag1 = true; } else if (tokenProviderException != null && tokenProviderException.InnerException != null && tokenProviderException.InnerException is WebException) { flag1 = true; } else if (lastAsyncStepException != null) { TokenProviderHelper.InternalSecurityTokenException internalSecurityTokenException = lastAsyncStepException as TokenProviderHelper.InternalSecurityTokenException; flag1 = true; if (internalSecurityTokenException != null) { base.LastAsyncStepException = new SecurityTokenException(internalSecurityTokenException.Message, internalSecurityTokenException.InnerException); switch (internalSecurityTokenException.StatusCode) { case HttpStatusCode.BadRequest: case HttpStatusCode.Unauthorized: { flag1 = false; break; } } } } if (flag1) { TimeSpan timeSpan1 = base.RemainingTime(); if (timeSpan1 <= TimeSpan.Zero) { flag1 = false; } else { yield return(base.CallAsyncSleep(TimeoutHelper.Min(this.retrySleepTime, timeSpan1))); TimeSpan timeSpan2 = this.retrySleepTime.Add(this.retrySleepTime); TokenProvider.GetTokenAsyncResultBase <T> getTokenAsyncResultBase1 = this; timeSpan = (timeSpan2 < TokenProvider.MaxRetrySleepTime ? timeSpan2 : TokenProvider.MaxRetrySleepTime); getTokenAsyncResultBase1.retrySleepTime = timeSpan; } } if (!flag1) { if (base.LastAsyncStepException == null) { break; } base.Complete(base.LastAsyncStepException); break; } } }