private static string ExtractAudience(string token)
        {
            string str = null;

            if (token != null)
            {
                string[] strArrays = token.Split(new char[] { '&' });
                for (int i = 0; i < (int)strArrays.Length; i++)
                {
                    string   str1       = strArrays[i];
                    string[] strArrays1 = str1.Split(new char[] { '=' });
                    if ((int)strArrays1.Length != 2)
                    {
                        string   tokenProviderInvalidTokenParameter = Resources.TokenProviderInvalidTokenParameter;
                        object[] objArray = new object[] { str1 };
                        TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(tokenProviderInvalidTokenParameter, objArray));
                    }
                    else if (strArrays1[0].Trim() == "Audience")
                    {
                        str = HttpUtility.UrlDecode(strArrays1[1].Trim());
                    }
                }
            }
            if (string.IsNullOrEmpty(str))
            {
                TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyAudience, new object[0]));
            }
            return(str);
        }
 private static void ExtractExpiresInAndAudience(string simpleWebToken, out DateTime expiresIn, out string audience)
 {
     expiresIn = DateTime.MinValue;
     audience  = null;
     if (string.IsNullOrWhiteSpace(simpleWebToken))
     {
         TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyToken, new object[0]));
     }
     else
     {
         IDictionary <string, string> strs = TokenProviderHelper.Decode(simpleWebToken);
         string item = strs["ExpiresOn"];
         if (string.IsNullOrWhiteSpace(item))
         {
             TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyExpiration, new object[0]));
         }
         expiresIn = TokenConstants.WrapBaseTime + TimeSpan.FromSeconds(double.Parse(HttpUtility.UrlDecode(item.Trim()), CultureInfo.InvariantCulture));
         audience  = strs["Audience"];
         if (string.IsNullOrWhiteSpace(item))
         {
             TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyAudience, new object[0]));
             return;
         }
     }
 }
        public static string GetWindowsAccessTokenCore(IEnumerator <Uri> stsUris, Func <Uri, Uri> uriBuilder, string requestToken, TimeSpan timeout, out DateTime expiresIn, out string audience)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(requestToken);
            string end   = null;

            expiresIn = DateTime.MinValue;
            audience  = null;
            bool flag = stsUris.MoveNext();

            while (flag)
            {
                Uri uri = uriBuilder(stsUris.Current);
                try
                {
                    HttpWebRequest servicePointMaxIdleTimeMilliSeconds = WebRequest.Create(uri) as HttpWebRequest;
                    servicePointMaxIdleTimeMilliSeconds.ServicePoint.MaxIdleTime     = Constants.ServicePointMaxIdleTimeMilliSeconds;
                    servicePointMaxIdleTimeMilliSeconds.AllowAutoRedirect            = true;
                    servicePointMaxIdleTimeMilliSeconds.MaximumAutomaticRedirections = 1;
                    servicePointMaxIdleTimeMilliSeconds.Method                        = "POST";
                    servicePointMaxIdleTimeMilliSeconds.ContentType                   = "application/x-www-form-urlencoded";
                    servicePointMaxIdleTimeMilliSeconds.ContentLength                 = (long)((int)bytes.Length);
                    servicePointMaxIdleTimeMilliSeconds.Timeout                       = TokenProviderHelper.ConvertToInt32(timeout);
                    servicePointMaxIdleTimeMilliSeconds.UseDefaultCredentials         = true;
                    AuthenticationManager.CustomTargetNameDictionary[uri.AbsoluteUri] = "HTTP\\";
                    using (Stream requestStream = servicePointMaxIdleTimeMilliSeconds.GetRequestStream())
                    {
                        requestStream.Write(bytes, 0, (int)bytes.Length);
                    }
                    using (HttpWebResponse response = servicePointMaxIdleTimeMilliSeconds.GetResponse() as HttpWebResponse)
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
                            {
                                end = streamReader.ReadToEnd();
                                TokenProviderHelper.ExtractExpiresInAndAudience(end, out expiresIn, out audience);
                            }
                        }
                    }
                    flag = false;
                }
                catch (ArgumentException argumentException)
                {
                    TokenProviderHelper.ThrowException(uri, argumentException);
                }
                catch (WebException webException1)
                {
                    WebException webException = webException1;
                    flag = stsUris.MoveNext();
                    if (!flag)
                    {
                        TokenProviderHelper.ThrowException(uri, webException);
                    }
                }
            }
            return(end);
        }
 private static void ExtractAccessToken(string urlParameters, out string token, out string expiresIn, out string audience)
 {
     token     = null;
     expiresIn = null;
     audience  = null;
     if (urlParameters != null)
     {
         string[] strArrays = urlParameters.Split(new char[] { '&' });
         for (int i = 0; i < (int)strArrays.Length; i++)
         {
             string   str        = strArrays[i];
             string[] strArrays1 = str.Split(new char[] { '=' });
             if ((int)strArrays1.Length != 2)
             {
                 string   tokenProviderInvalidTokenParameter = Resources.TokenProviderInvalidTokenParameter;
                 object[] objArray = new object[] { str };
                 TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(tokenProviderInvalidTokenParameter, objArray));
             }
             else
             {
                 string str1 = strArrays1[0].Trim();
                 if (str1 == "wrap_access_token")
                 {
                     token    = HttpUtility.UrlDecode(strArrays1[1].Trim());
                     audience = TokenProviderHelper.ExtractAudience(token);
                 }
                 else if (str1 == "wrap_access_token_expires_in")
                 {
                     expiresIn = HttpUtility.UrlDecode(strArrays1[1].Trim());
                 }
             }
         }
     }
     if (string.IsNullOrEmpty(token))
     {
         TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyToken, new object[0]));
     }
     if (string.IsNullOrEmpty(expiresIn))
     {
         TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyExpiration, new object[0]));
     }
     if (string.IsNullOrEmpty(audience))
     {
         TokenProviderHelper.ThrowException(Microsoft.ServiceBus.SR.GetString(Resources.TokenProviderEmptyAudience, new object[0]));
     }
 }
        private static string GetAccessTokenCore(Uri requestUri, string appliesTo, string requestToken, string simpleAuthAssertionFormat, TimeSpan timeout, out string expiresIn, out string audience)
        {
            StringBuilder stringBuilder    = new StringBuilder();
            CultureInfo   invariantCulture = CultureInfo.InvariantCulture;

            object[] objArray = new object[] { "wrap_scope", HttpUtility.UrlEncode(appliesTo) };
            stringBuilder.AppendFormat(invariantCulture, "{0}={1}", objArray);
            stringBuilder.Append('&');
            CultureInfo cultureInfo = CultureInfo.InvariantCulture;

            object[] objArray1 = new object[] { "wrap_assertion_format", simpleAuthAssertionFormat };
            stringBuilder.AppendFormat(cultureInfo, "{0}={1}", objArray1);
            stringBuilder.Append('&');
            CultureInfo invariantCulture1 = CultureInfo.InvariantCulture;

            object[] objArray2 = new object[] { "wrap_assertion", HttpUtility.UrlEncode(requestToken) };
            stringBuilder.AppendFormat(invariantCulture1, "{0}={1}", objArray2);
            byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
            string str   = null;

            expiresIn = null;
            audience  = null;
            try
            {
                HttpWebRequest servicePointMaxIdleTimeMilliSeconds = WebRequest.Create(requestUri) as HttpWebRequest;
                servicePointMaxIdleTimeMilliSeconds.ServicePoint.MaxIdleTime     = Constants.ServicePointMaxIdleTimeMilliSeconds;
                servicePointMaxIdleTimeMilliSeconds.AllowAutoRedirect            = true;
                servicePointMaxIdleTimeMilliSeconds.MaximumAutomaticRedirections = 1;
                servicePointMaxIdleTimeMilliSeconds.Method        = "POST";
                servicePointMaxIdleTimeMilliSeconds.ContentType   = "application/x-www-form-urlencoded";
                servicePointMaxIdleTimeMilliSeconds.ContentLength = (long)((int)bytes.Length);
                try
                {
                    servicePointMaxIdleTimeMilliSeconds.Timeout = Convert.ToInt32(timeout.TotalMilliseconds, CultureInfo.InvariantCulture);
                }
                catch (OverflowException overflowException)
                {
                    throw new ArgumentException(SRClient.TimeoutExceeded, overflowException);
                }
                using (Stream requestStream = servicePointMaxIdleTimeMilliSeconds.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, (int)bytes.Length);
                }
                using (HttpWebResponse response = (HttpWebResponse)servicePointMaxIdleTimeMilliSeconds.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            TokenProviderHelper.ExtractAccessToken(streamReader.ReadToEnd(), out str, out expiresIn, out audience);
                        }
                    }
                }
            }
            catch (ArgumentException argumentException)
            {
                TokenProviderHelper.ThrowException(requestUri, argumentException);
            }
            catch (WebException webException)
            {
                TokenProviderHelper.ThrowException(requestUri, webException);
            }
            return(str);
        }