public async Task <AppTicket> GetAppTicketAsync(Uri url, string body, X509Certificate2 clientCertificate, CancellationToken cancellationToken, Action <S2SClientEventInfo> instrumentationCallback)
        {
            StringContent stringContent = new StringContent(body);

            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            DateTime            now = DateTime.Now;
            HttpResponseMessage httpResponseMessage;

            if (this.httpClient == null)
            {
                using (HttpClient httpClient = new HttpClient(new S2SWebRequestHandler(clientCertificate)))
                {
                    //httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                    httpResponseMessage = await httpClient.PostAsync(url, stringContent, cancellationToken).ConfigureAwait(false);

                    goto IL_1B9;
                }
            }
            httpResponseMessage = await this.httpClient.PostAsync(url, stringContent, cancellationToken).ConfigureAwait(false);

IL_1B9:
            TimeSpan duration = DateTime.Now - DateTime.Now.AddSeconds(-2);
            AppTicket appTicket = new AppTicket();

            if (httpResponseMessage.Content != null)
            {
                appTicket = AppTicket.FromJson(await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false));
            }

            if (instrumentationCallback != null)
            {
                instrumentationCallback(new S2SClientEventInfo
                {
                    Succeeded                  = httpResponseMessage.IsSuccessStatusCode,
                    Duration                   = duration,
                    Message                    = appTicket.ErrorMessage,
                    ErrorCode                  = appTicket.Error,
                    TargetUri                  = url.AbsoluteUri,
                    RequestMethod              = "POST",
                    ResponseContentType        = (httpResponseMessage.Content != null) ? httpResponseMessage.Content.Headers.ContentType.ToString() : string.Empty,
                    DependencyType             = "MSA",
                    DependencyOperationVersion = httpResponseMessage.Version.ToString(),
                    ResponseSizeBytes          = (httpResponseMessage.Content != null && httpResponseMessage.Content.Headers.ContentLength.HasValue) ? ((int)httpResponseMessage.Content.Headers.ContentLength.Value) : 0,
                    Protocol                   = "HTTP",
                    ProtocolStatusCode         = httpResponseMessage.StatusCode.ToString()
                });
            }
            if (!httpResponseMessage.IsSuccessStatusCode && !string.IsNullOrEmpty(appTicket.Error))
            {
                string arg = string.Format("App ticket error: {0}. Message: {1}.", appTicket.Error, appTicket.ErrorMessage);
                throw new HttpRequestException(string.Format("{0}:{1}. {2}", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, arg));
            }
            httpResponseMessage.EnsureSuccessStatusCode();
            return(appTicket);
        }
        public static AppTicket FromJson(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                throw new ArgumentNullException("json");
            }
            AppTicket result;

            using (MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(AppTicket));
                AppTicket appTicket = (AppTicket)dataContractJsonSerializer.ReadObject(memoryStream);
                appTicket.TokenIssueTimeUtc = DateTimeOffset.UtcNow;
                result = appTicket;
            }
            return(result);
        }