Пример #1
0
        /// <summary>
        /// Generate the XML for a request.
        /// </summary>
        /// <param name="clientRequest">The request.</param>
        /// <returns>The XML representation.</returns>
        internal string GenerateRequestXml(HealthVaultRequest clientRequest)
        {
            XElement request = XElement.Parse(@"<wc-request:request xmlns:wc-request=""urn:com.microsoft.wc.request"" />");

            XElement header = new XElement("header");
            {
                header.Add(new XElement("method", clientRequest.MethodName));
                header.Add(new XElement("method-version", clientRequest.MethodVersion));

                if (CurrentRecord != null)
                {
                    header.Add(new XElement("record-id", CurrentRecord.RecordId.ToString()));
                }

                if (!String.IsNullOrEmpty(AuthorizationSessionToken))
                {
                    XElement authSession = new XElement("auth-session");
                    authSession.Add(new XElement("auth-token", AuthorizationSessionToken));

                    if (CurrentRecord != null)
                    {
                        authSession.Add(new XElement("offline-person-info",
                                                     new XElement("offline-person-id", CurrentRecord.PersonId.ToString())));
                    }

                    header.Add(authSession);
                }
                else
                {
                    if (AppIdInstance == Guid.Empty)
                    {
                        header.Add(new XElement("app-id", MasterAppId.ToString()));
                    }
                    else
                    {
                        header.Add(new XElement("app-id", AppIdInstance.ToString()));
                    }
                }

                header.Add(new XElement("language", Language));
                header.Add(new XElement("country", Country));
                header.Add(new XElement("msg-time", clientRequest.MessageTime.ToUniversalTime().ToString("O")));
                header.Add(new XElement("msg-ttl", "1800"));
                header.Add(new XElement("version", MobilePlatform.PlatformAbbreviationAndVersion));
            }

            XElement info = new XElement("info");

            if (clientRequest.InfoSection != null)
            {
                info = clientRequest.InfoSection;
            }

            if (clientRequest.MethodName != "CreateAuthenticatedSessionToken")
            {
                // if we have an info section, we need to compute the hash of that and put it in the header.
                if (clientRequest.InfoSection != null)
                {
                    string infoString = GetOuterXml(info);
                    header.Add(new XElement("info-hash",
                                            MobilePlatform.ComputeSha256HashAndWrap(infoString)));
                }

                if (!String.IsNullOrEmpty(SessionSharedSecret))
                {
                    byte[] sharedSecretKey = Convert.FromBase64String(SessionSharedSecret);
                    string headerXml       = GetOuterXml(header);

                    request.Add(new XElement("auth",
                                             MobilePlatform.ComputeSha256HmacAndWrap(sharedSecretKey, headerXml)));
                }
            }

            request.Add(header);
            request.Add(info);

            string requestString = GetOuterXml(request);

            return(requestString);
        }