public void ToClientCredentialsTest()
        {
            DeviceUserName    deviceUserName    = new DeviceUserName();
            ClientCredentials clientCredentials = deviceUserName.ToClientCredentials();

            Assert.IsNotNull(clientCredentials);
        }
Example #2
0
    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 userName;

            if (string.IsNullOrWhiteSpace(deviceName))
            {
                userName = DeviceIdManager.GenerateDeviceUserName();
            }
            else
            {
                userName = new DeviceUserName
                {
                    DeviceName        = deviceName,
                    DecryptedPassword = devicePassword
                };
            }
            return(DeviceIdManager.RegisterDevice(applicationId, issuerUri, userName));
        }
            private static LiveDevice GenerateDevice(string deviceName, string devicePassword)
            {
                // If the deviceName hasn't been specified, it should be generated using random characters.
                DeviceUserName userNameCredentials;

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

                return(new LiveDevice()
                {
                    User = userNameCredentials, Version = 1
                });
            }
Example #5
0
    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);
    }
        private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, DeviceUserName userName)
        {
            var text       = DeviceIdManager.DiscoverEnvironment(issuerUri);
            var liveDevice = new LiveDevice
            {
                User    = userName,
                Version = 1
            };
            var registrationRequest        = new DeviceRegistrationRequest(applicationId, liveDevice);
            var deviceRegistrationResponse = DeviceIdManager.ExecuteRegistrationRequest(string.Format(CultureInfo.InvariantCulture, "https://login.live{0}.com/ppsecure/DeviceAddCredential.srf", string.IsNullOrWhiteSpace(text) ? null : ("-" + text)), registrationRequest);

            if (!deviceRegistrationResponse.IsSuccess)
            {
                throw new DeviceRegistrationFailedException((Microsoft.Xrm.Tooling.Connector.DeviceRegistrationErrorCode)deviceRegistrationResponse.Error.RegistrationErrorCode, deviceRegistrationResponse.ErrorSubCode);
            }
            DeviceIdManager.WriteDevice(text, liveDevice);
            return(liveDevice.User.ToClientCredentials());
        }