private static DeviceUserName GenerateDeviceUserName()
        {
            DeviceUserName userName = new DeviceUserName();

            userName.DeviceName        = GenerateRandomString(LiveIdConstants.ValidDeviceNameCharacters, LiveIdConstants.DeviceNameLength);
            userName.DecryptedPassword = GenerateRandomString(LiveIdConstants.ValidDevicePasswordCharacters, LiveIdConstants.DevicePasswordLength);

            return(userName);
        }
        /// <summary>
        /// Registers the given device with Live ID
        /// </summary>
        /// <param name="applicationId">ID for the application</param>
        /// <param name="issuerUri">URL for the current token issuer</param>
        /// <param name="deviceName">Device name that should be registered</param>
        /// <param name="devicePassword">Device password that should be registered</param>
        /// <returns>ClientCredentials that were registered</returns>
        /// <remarks>
        /// The issuerUri can be retrieved from the IServiceConfiguration interface's CurrentIssuer property.
        /// </remarks>
        public static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, string deviceName, string devicePassword)
        {
            if (string.IsNullOrWhiteSpace(deviceName) != string.IsNullOrWhiteSpace(devicePassword))
            {
                throw new ArgumentNullException("deviceName", "Either deviceName/devicePassword should both be specified or they should be null.");
            }

            DeviceUserName userNameCredentials;

            if (string.IsNullOrWhiteSpace(deviceName))
            {
                userNameCredentials = GenerateDeviceUserName();
            }
            else
            {
                userNameCredentials = new DeviceUserName()
                {
                    DeviceName = deviceName, DecryptedPassword = devicePassword
                };
            }

            return(RegisterDevice(applicationId, issuerUri, userNameCredentials));
        }
        private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, DeviceUserName userName)
        {
            bool doContinue = true;
            int  attempt    = 1;

            while (doContinue)
            {
                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.IsNullOrWhiteSpace(environment) ? null : "-" + environment);


                try
                {
                    DeviceRegistrationResponse response = ExecuteRegistrationRequest(url, request);
                    if (!response.IsSuccess)
                    {
                        throw new DeviceRegistrationFailedException(response.RegistrationErrorCode.GetValueOrDefault(), response.ErrorSubCode);
                    }

                    WriteDevice(environment, device);
                }
                catch (Exception error)
                {
                    if (error.Message.ToLower().Contains("unknown"))
                    {
                        if (attempt > 3)
                        {
                            if (MessageBox.Show("Failed to connect 3 times.\r\n\r\nDo you want to retry?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                            {
                                doContinue = false;
                            }
                        }

                        attempt++;
                    }
                    else
                    {
                        throw error;
                    }
                }

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

            return(null);
        }