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, LiveDevice device) { EnvironmentConfiguration environment = DiscoverEnvironmentInternal(issuerUri); DeviceRegistrationRequest request = new DeviceRegistrationRequest(applicationId, device); string url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.RegistrationEndpointUriFormat, environment.HostName); DeviceRegistrationResponse response = ExecuteRegistrationRequest(url, request); if (!response.IsSuccess) { bool 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()); }