Ejemplo n.º 1
0
        private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, DeviceUserName userName)
        {
            string environment = DiscoverEnvironment(issuerUri);

            LiveDevice device = new LiveDevice()
            {
                User = userName, Version = 1
            };

            DeviceRegistrationRequest request = new DeviceRegistrationRequest(applicationId, device);

            string url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.RegistrationEndpointUriFormat,
                                       string.IsNullOrEmpty(environment) ? null : "-" + environment);

            DeviceRegistrationResponse response = ExecuteRegistrationRequest(url, request);

            if (!response.IsSuccess)
            {
                //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.
                if (!PersistToFile && DeviceRegistrationErrorCode.DeviceAlreadyExists == response.Error.RegistrationErrorCode)
                {
                    return(device.User.ToClientCredentials());
                }

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

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

            return(device.User.ToClientCredentials());
        }
Ejemplo n.º 2
0
        private static DeviceRegistrationResponse ExecuteRegistrationRequest(string url, DeviceRegistrationRequest registrationRequest)
        {
            //Create the request that will submit the request to the server
            WebRequest 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 (Stream stream = request.GetRequestStream())
            {
                Serialize(stream, registrationRequest);
            }

            // Read the response into an XmlDocument and return that doc
            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        return(Deserialize <DeviceRegistrationResponse>(stream));
                    }
                }
            }
            catch (WebException ex)
            {
                if (null != ex.Response)
                {
                    using (Stream stream = ex.Response.GetResponseStream())
                    {
                        return(Deserialize <DeviceRegistrationResponse>(stream));
                    }
                }

                throw;
            }
        }