protected override IEnumerator <IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .AsyncStep> GetAsyncSteps() { try { StringBuilder stringBuilder = new StringBuilder(); CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] objArray = new object[] { "wrap_scope", HttpUtility.UrlEncode(this.appliesTo) }; stringBuilder.AppendFormat(invariantCulture, "{0}={1}", objArray); stringBuilder.Append('&'); CultureInfo cultureInfo = CultureInfo.InvariantCulture; object[] objArray1 = new object[] { "wrap_assertion_format", this.simpleAuthAssertionFormat }; stringBuilder.AppendFormat(cultureInfo, "{0}={1}", objArray1); stringBuilder.Append('&'); CultureInfo invariantCulture1 = CultureInfo.InvariantCulture; object[] objArray2 = new object[] { "wrap_assertion", HttpUtility.UrlEncode(this.requestToken) }; stringBuilder.AppendFormat(invariantCulture1, "{0}={1}", objArray2); this.body = Encoding.UTF8.GetBytes(stringBuilder.ToString()); this.request = (HttpWebRequest)WebRequest.Create(this.requestUri); this.request.ServicePoint.MaxIdleTime = Constants.ServicePointMaxIdleTimeMilliSeconds; this.request.AllowAutoRedirect = true; this.request.MaximumAutomaticRedirections = 1; this.request.Method = "POST"; this.request.ContentType = "application/x-www-form-urlencoded"; this.request.ContentLength = (long)((int)this.body.Length); try { HttpWebRequest num = this.request; TimeSpan timeSpan = base.RemainingTime(); num.Timeout = Convert.ToInt32(timeSpan.TotalMilliseconds, CultureInfo.InvariantCulture); } catch (OverflowException overflowException) { throw new ArgumentException(SRClient.TimeoutExceeded, overflowException); } } catch (ArgumentException argumentException) { base.Complete(this.ConvertException(argumentException)); goto Label0; } try { this.requestCancelTimer = new IOThreadTimer(TokenProviderHelper.TokenRequestAsyncResult.onCancelTimer, this, true); try { TimeSpan timeSpan1 = base.RemainingTime(); if (timeSpan1 > TimeSpan.Zero) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall beginCall = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => { IAsyncResult asyncResult = thisPtr.request.BeginGetRequestStream(c, s); thisPtr.requestCancelTimer.Set(timeSpan1); return(asyncResult); }; yield return(tokenRequestAsyncResult.CallAsync(beginCall, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => thisPtr.requestStream = thisPtr.request.EndGetRequestStream(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult1 = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall beginCall1 = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.requestStream.BeginWrite(thisPtr.body, 0, (int)thisPtr.body.Length, c, s); yield return(tokenRequestAsyncResult1.CallAsync(beginCall1, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => thisPtr.requestStream.EndWrite(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { goto Label1; } base.Complete(this.ConvertException(base.LastAsyncStepException)); goto Label0; } else { base.Complete(this.ConvertException(base.LastAsyncStepException)); goto Label0; } } else { base.Complete(new TimeoutException(SRClient.OperationRequestTimedOut(base.OriginalTimeout))); goto Label0; } } finally { if (this.requestStream != null) { this.requestStream.Dispose(); } this.requestCancelTimer.Cancel(); } Label1: TimeSpan timeSpan2 = base.RemainingTime(); if (timeSpan2 > TimeSpan.Zero) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult2 = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall beginCall2 = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => { IAsyncResult asyncResult = thisPtr.request.BeginGetResponse(c, s); thisPtr.requestCancelTimer.Set(timeSpan2); return(asyncResult); }; yield return(tokenRequestAsyncResult2.CallAsync(beginCall2, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => thisPtr.response = (HttpWebResponse)thisPtr.request.EndGetResponse(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult3 = this; Stream responseStream = this.response.GetResponseStream(); Stream stream = responseStream; tokenRequestAsyncResult3.sourceStream = responseStream; using (stream) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult4 = this; MemoryStream memoryStream = new MemoryStream(); MemoryStream memoryStream1 = memoryStream; tokenRequestAsyncResult4.tmpStream = memoryStream; using (memoryStream1) { TokenProviderHelper.TokenRequestAsyncResult tokenRequestAsyncResult5 = this; IteratorAsyncResult <TokenProviderHelper.TokenRequestAsyncResult> .BeginCall streamReaderAsyncResult = (TokenProviderHelper.TokenRequestAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => new TokenProviderHelper.StreamReaderAsyncResult(thisPtr.sourceStream, thisPtr.tmpStream, t, c, s); yield return(tokenRequestAsyncResult5.CallAsync(streamReaderAsyncResult, (TokenProviderHelper.TokenRequestAsyncResult thisPtr, IAsyncResult r) => AsyncResult <TokenProviderHelper.StreamReaderAsyncResult> .End(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue)); if (base.LastAsyncStepException == null) { try { this.tmpStream.Position = (long)0; using (StreamReader streamReader = new StreamReader(this.tmpStream, Encoding.UTF8)) { string end = streamReader.ReadToEnd(); TokenProviderHelper.ExtractAccessToken(end, out this.accessToken, out this.expiresIn, out this.audience); } } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } base.Complete(this.ConvertException(exception)); } } else { base.Complete(this.ConvertException(base.LastAsyncStepException)); } } } } else { base.Complete(this.ConvertException(base.LastAsyncStepException)); } } else { base.Complete(new TimeoutException(SRClient.OperationRequestTimedOut(base.OriginalTimeout))); } } finally { if (this.requestCancelTimer != null) { this.requestCancelTimer.Cancel(); } } Label0: yield break; }
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); }