/// <summary>
        /// Retrieves token information.
        /// </summary>
        /// 
        /// <param name="applicationTokenCreationInfo">
        /// The application token creation information.
        /// </param>
        /// 
        /// <param name="stsOriginalRequestUrl">
        /// The original URL of the STS request.
        /// </param>
        /// 
        /// <returns>
        /// An XML string representing the token information.
        /// </returns>
        /// 
        private string ConstructCreateTokenInfoXml(
            ApplicationTokenCreationInfo applicationTokenCreationInfo,
            string stsOriginalRequestUrl)
        {
            StringBuilder infoXml = new StringBuilder(128);
            XmlWriterSettings settings = SDKHelper.XmlUnicodeWriterSettings;
            XmlWriter writer = null;

            try
            {
                writer = XmlWriter.Create(infoXml, settings);

                // Add the PersonInfo elements
                writer.WriteStartElement("auth-info");

                //app-id
                ConstructCreateTokenInfoXmlAppIdPart(
                    writer,
                    applicationTokenCreationInfo);

                // <credentials>
                writer.WriteStartElement("credential");
                WriteInfoXml(writer);
                writer.WriteEndElement();

                // <sts-info>
                if (stsOriginalRequestUrl != null)
                {
                    writer.WriteStartElement("sts-info");
                    writer.WriteElementString("original-request-url", stsOriginalRequestUrl);
                    writer.WriteEndElement();
                }

                // <second factor credential>

                // Close the auth-info tag
                writer.WriteEndElement();
                writer.Flush();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            return (infoXml.ToString());
        }
        /// <summary>
        /// Create authentication tokens and absence reasons for user 
        /// credentials, given the specified method, connection, and multiple 
        /// application ID's.
        /// </summary>
        /// 
        /// <remarks>
        /// The Shell primarily uses multiple application ID's when 
        /// constructing simple credential tokens: One each for the Shell and 
        /// for the target application.
        /// </remarks>
        /// 
        /// <param name="methodName">
        /// The HealthVault service method name to call.
        /// </param>
        /// 
        /// <param name="version">
        /// The version of the service method to call.
        /// </param>
        /// 
        /// <param name="connection">
        /// The <see cref="HealthServiceConnection"/> instance.
        /// </param>
        /// 
        /// <param name="applicationTokenCreationInfo">
        /// The application token creation information.
        /// </param>
        /// 
        /// <param name="stsOriginalUrl">
        /// The original url from the STS request.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="methodName"/>
        /// parameter is <b>null</b> or empty.
        /// </exception>
        /// 
        internal void MakeCreateTokenCall(
            string methodName,
            int version,
            HealthServiceConnection connection,
            ApplicationTokenCreationInfo applicationTokenCreationInfo,
            string stsOriginalUrl)
        {
            Validator.ThrowIfStringNullOrEmpty(methodName, "CreateTokenMethodNameIsNullOrEmpty");

            Validator.ThrowArgumentExceptionIf(
                applicationTokenCreationInfo == null,
                "appTokenCreationInfo",
                "AuthenticationAppIDCollectionNullOrEmpty");

            if (IsTokenCached(applicationTokenCreationInfo))
            {
                return;
            }

            AuthenticationMethodName = methodName;

            HealthServiceRequest request =
                new HealthServiceRequest(connection, AuthenticationMethodName, version);

            request.Parameters =
                ConstructCreateTokenInfoXml(applicationTokenCreationInfo, stsOriginalUrl);

            request.Execute();

            CreateAuthenticationTokenResult createAuthTokenResult =
                GetAuthTokenAndAbsenceReasons(
                    request.Response.InfoNavigator);

            ParseExtendedElements(request.Response.InfoNavigator);

            UpdateAuthenticationResults(createAuthTokenResult);
        }
        /// <summary>
        /// Creates the specified individual values of application identifiers 
        /// in the specified collection.
        /// </summary>
        /// 
        /// <param name="writer">
        /// The XmlWriter receiving the values.
        /// </param>
        /// 
        /// <param name="applicationTokenCreationInfo">
        /// The application token creation information.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="writer"/> parameter is <b>null</b>.
        /// </exception>
        /// 
        private static void ConstructCreateTokenInfoXmlAppIdPart(
            XmlWriter writer,
            ApplicationTokenCreationInfo applicationTokenCreationInfo)
        {
            Validator.ThrowIfArgumentNull(writer, "writer", "WriteXmlNullWriter");

            writer.WriteStartElement("app-id");

            if (applicationTokenCreationInfo.IsMRA)
            {
                writer.WriteAttributeString("is-multi-record-app", "true");
            }
            writer.WriteValue(applicationTokenCreationInfo.ApplicationId.ToString());
            writer.WriteEndElement();
        }
        /// <summary>
        /// Determines whether the results are in the cache.
        /// </summary>
        /// 
        /// <param name="applicationTokenCreationInfo">
        /// The application token creation information.
        /// </param>
        /// 
        /// <returns>
        /// True if there is already a token in the cache. 
        /// </returns>
        /// 
        internal virtual bool IsTokenCached(
            ApplicationTokenCreationInfo applicationTokenCreationInfo)
        {
            if (AuthenticationResults == null ||
                AuthenticationResults.Count == 0)
            {
                return false;
            }

            if (AuthenticationResults.ContainsKey(applicationTokenCreationInfo.ApplicationId))
            {
                return true;
            }

            return false;
        }