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()); } }
private static DeviceRegistrationResponse ExecuteRegistrationRequest(string url, DeviceRegistrationRequest registrationRequest) { //Create the request that will submit the request to the server var request = WebRequest.Create(url); request.ContentType = "application/soap+xml; charset=UTF-8"; request.Method = "POST"; request.Timeout = 180000; request.Proxy.Credentials = CredentialCache.DefaultCredentials; //Write the envelope to the RequestStream using (var stream = request.GetRequestStream()) { Serialize(stream, registrationRequest); } // Read the response into an XmlDocument and return that doc try { using (var response = request.GetResponse()) { using (var stream = response.GetResponseStream()) { return(Deserialize <DeviceRegistrationResponse>(stream)); } } } catch (WebException ex) { if (null != ex.Response) { using (var stream = ex.Response.GetResponseStream()) { return(Deserialize <DeviceRegistrationResponse>(stream)); } } throw; } }