private static DeviceUserName GenerateDeviceUserName() { var userName = new DeviceUserName(); userName.DeviceName = GenerateRandomString(LiveIdConstants.VALID_DEVICE_NAME_CHARACTERS, LiveIdConstants.DEVICE_NAME_LENGTH); userName.DecryptedPassword = GenerateRandomString(LiveIdConstants.VALID_DEVICE_PASSWORD_CHARACTERS, LiveIdConstants.DEVICE_PASSWORD_LENGTH); 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) { var attempt = 1; while (true) { var environment = DiscoverEnvironment(issuerUri); var device = new LiveDevice { User = userName, Version = 1 }; var request = new DeviceRegistrationRequest(applicationId, device); var url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.REGISTRATION_ENDPOINT_URI_FORMAT, string.IsNullOrWhiteSpace(environment) ? null : "-" + environment); try { var 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. Do you want to retry?", @"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { } } } else { throw; } } return(device.User.ToClientCredentials()); } }