private static DeviceRegistrationResponse ExecuteRegistrationRequest(string url,
                                                                             DeviceRegistrationRequest
                                                                             registrationRequest)
        {
            //Create the request that will submit the request to the server
            var request = WebRequest.Create(url);

            request.ContentType = "application/soap+xml; charset=UTF-8";
            request.Method      = "POST";
            request.Timeout     = 180000;

            //Write the envelope to the RequestStream
            using (var stream = request.GetRequestStream())
            {
                Serialize(stream, registrationRequest);
            }

            // Read the response into an XmlDocument and return that doc
            try
            {
                using (var response = request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        return(Deserialize <DeviceRegistrationResponse>("Deserializing Registration Response", stream));
                    }
                }
            }
            catch (WebException ex)
            {
                Trace.TraceError("Live ID Device Registration Failed (HTTP Code: {0}): {1}",
                                 ex.Status, ex.Message);

                if (null != ex.Response)
                {
                    using (var stream = ex.Response.GetResponseStream())
                    {
                        return(Deserialize <DeviceRegistrationResponse>("Deserializing Failed Registration Response",
                                                                        stream));
                    }
                }

                throw;
            }
        }
        private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, LiveDevice device)
        {
            var environment = DiscoverEnvironmentInternal(issuerUri);

            var request = new DeviceRegistrationRequest(applicationId, device);

            var url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.RegistrationEndpointUriFormat,
                                    environment.HostName);

            var response = ExecuteRegistrationRequest(url, request);

            if (!response.IsSuccess)
            {
                var throwException = true;
                if (DeviceRegistrationErrorCode.DeviceAlreadyExists == response.Error.RegistrationErrorCode)
                {
                    if (!PersistToFile)
                    {
                        //If the file is not persisted, the registration will always occur (since the credentials are not
                        //persisted to the disk. However, the credentials may already exist. To avoid an exception being continually
                        //processed by the calling user, DeviceAlreadyExists will be ignored if the credentials are not persisted to the disk.
                        return(device.User.ToClientCredentials());
                    }
                    else if (PersistIfDeviceAlreadyExists)
                    {
                        // This flag indicates that the
                        throwException = false;
                    }
                }

                if (throwException)
                {
                    throw new DeviceRegistrationFailedException(response.Error.RegistrationErrorCode,
                                                                response.ErrorSubCode);
                }
            }

            if (PersistToFile || PersistIfDeviceAlreadyExists)
            {
                WriteDevice(environment, device);
            }

            return(device.User.ToClientCredentials());
        }