internal static SecurityToken GetMessagingToken(this TokenProvider tokenProvider, Uri namespaceAddress, string appliesTo, string action, bool bypassCache, TimeSpan timeout)
        {
            SecurityToken securityToken;

            try
            {
                TimeSpan timeSpan = TimeoutHelper.Min(timeout, TokenProviderUtility.MaxGetTokenSyncTimeout);
                securityToken = tokenProvider.EndGetToken(tokenProvider.BeginGetToken(namespaceAddress, appliesTo, action, bypassCache, timeSpan, null, null));
            }
            catch (TimeoutException timeoutException)
            {
                throw;
            }
            catch (TokenProviderException tokenProviderException1)
            {
                TokenProviderException tokenProviderException = tokenProviderException1;
                throw new MessagingException(tokenProviderException.Message, tokenProviderException);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (!Fx.IsFatal(exception))
                {
                    throw new UnauthorizedAccessException(exception.Message, exception);
                }
                throw;
            }
            return(securityToken);
        }
        internal static string GetMessagingWebToken(this TokenProvider tokenProvider, Uri namespaceAddress, string appliesTo, string action, bool bypassCache, TimeSpan timeout)
        {
            string str;

            try
            {
                str = tokenProvider.EndGetWebToken(tokenProvider.BeginGetWebToken(namespaceAddress, appliesTo, action, bypassCache, timeout, null, null));
            }
            catch (TimeoutException timeoutException)
            {
                throw;
            }
            catch (TokenProviderException tokenProviderException1)
            {
                TokenProviderException tokenProviderException = tokenProviderException1;
                throw new MessagingException(tokenProviderException.Message, tokenProviderException);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (!Fx.IsFatal(exception))
                {
                    throw new UnauthorizedAccessException(exception.Message, exception);
                }
                throw;
            }
            return(str);
        }
Exemplo n.º 3
0
            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;
                    }
                }
            }
        private static Exception ConvertWebException(Uri requestUri, WebException exception)
        {
            Exception securityTokenException;
            Exception exception1;

            object[] absoluteUri;
            object[] objArray;
            string   tokenProviderFailedSecurityToken;
            string   tokenProviderTimeout;
            string   empty = string.Empty;

            if (exception == null || exception.Response == null)
            {
                empty = (exception == null ? "Unknown" : exception.Message);
            }
            else
            {
                using (exception.Response)
                {
                    using (StreamReader streamReader = new StreamReader(exception.Response.GetResponseStream()))
                    {
                        empty = streamReader.ReadToEnd();
                    }
                    HttpWebResponse response = exception.Response as HttpWebResponse;
                    if (response == null || response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Continue)
                    {
                        tokenProviderFailedSecurityToken = Resources.TokenProviderFailedSecurityToken;
                        absoluteUri            = new object[] { requestUri.AbsoluteUri, empty };
                        securityTokenException = new SecurityTokenException(Microsoft.ServiceBus.SR.GetString(tokenProviderFailedSecurityToken, absoluteUri), exception);
                        if (exception != null && (exception.Status == WebExceptionStatus.Timeout || exception.Status == WebExceptionStatus.RequestCanceled))
                        {
                            tokenProviderTimeout   = Resources.TokenProviderTimeout;
                            objArray               = new object[] { requestUri.AbsoluteUri };
                            securityTokenException = new TimeoutException(Microsoft.ServiceBus.SR.GetString(tokenProviderTimeout, objArray), securityTokenException);
                        }
                        if (DiagnosticUtility.ShouldTraceInformation)
                        {
                            DiagnosticUtility.ExceptionUtility.TraceHandledException(securityTokenException, TraceEventType.Information);
                        }
                        return(securityTokenException);
                    }
                    else
                    {
                        string         str          = Resources.TokenProviderFailedSecurityToken;
                        object[]       absoluteUri1 = new object[] { requestUri.AbsoluteUri, empty };
                        Exception      internalSecurityTokenException = new TokenProviderHelper.InternalSecurityTokenException(Microsoft.ServiceBus.SR.GetString(str, absoluteUri1), response.StatusCode, exception);
                        HttpStatusCode statusCode = response.StatusCode;
                        if (statusCode != HttpStatusCode.NotFound)
                        {
                            if (statusCode != HttpStatusCode.RequestTimeout)
                            {
                                switch (statusCode)
                                {
                                case HttpStatusCode.BadGateway:
                                case HttpStatusCode.ServiceUnavailable:
                                {
                                    goto Label2;
                                }

                                case HttpStatusCode.GatewayTimeout:
                                {
                                    break;
                                }

                                default:
                                {
                                    goto Label1;
                                }
                                }
                            }
                            string   tokenProviderTimeout1 = Resources.TokenProviderTimeout;
                            object[] objArray1             = new object[] { requestUri.AbsoluteUri };
                            internalSecurityTokenException = new TimeoutException(Microsoft.ServiceBus.SR.GetString(tokenProviderTimeout1, objArray1), exception);
                            goto Label1;
                        }
Label2:
                        string tokenProviderServiceUnavailable = Resources.TokenProviderServiceUnavailable;
                        object[] absoluteUri2 = new object[] { requestUri.AbsoluteUri };
                        internalSecurityTokenException = new TokenProviderException(Microsoft.ServiceBus.SR.GetString(tokenProviderServiceUnavailable, absoluteUri2), exception);
Label1:
                        if (DiagnosticUtility.ShouldTraceInformation)
                        {
                            DiagnosticUtility.ExceptionUtility.TraceHandledException(internalSecurityTokenException, TraceEventType.Information);
                        }
                        exception1 = internalSecurityTokenException;
                    }
                }
                return(exception1);
            }
            tokenProviderFailedSecurityToken = Resources.TokenProviderFailedSecurityToken;
            absoluteUri            = new object[] { requestUri.AbsoluteUri, empty };
            securityTokenException = new SecurityTokenException(Microsoft.ServiceBus.SR.GetString(tokenProviderFailedSecurityToken, absoluteUri), exception);
            if (exception != null && (exception.Status == WebExceptionStatus.Timeout || exception.Status == WebExceptionStatus.RequestCanceled))
            {
                tokenProviderTimeout   = Resources.TokenProviderTimeout;
                objArray               = new object[] { requestUri.AbsoluteUri };
                securityTokenException = new TimeoutException(Microsoft.ServiceBus.SR.GetString(tokenProviderTimeout, objArray), securityTokenException);
            }
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(securityTokenException, TraceEventType.Information);
            }
            return(securityTokenException);
        }