private async Task <bool> TryAuthenticateAsync() { try { // Create portal object portal = new DevicePortal( new DefaultDevicePortalConnection( "https://127.0.0.1", UserNameBox.Text, PasswordBox.Password)); // Get cert (OK to use untrusted since it's loopback) await portal.GetRootDeviceCertificate(acceptUntrustedCerts : true); // Attempt to connect await portal.Connect(); // Get IPD var ipd = await portal.GetInterPupilaryDistance(); // Success! return(true); } catch (Exception) { // Problem await new MessageDialog("Authentication was not successful. Please make sure Device Portal is enabled and check your password.", "Error").ShowAsync(); return(false); } }
/// <summary> /// Click handler for the connectToDevice button. /// </summary> /// <param name="sender">The caller of this method.</param> /// <param name="e">The arguments associated with this event.</param> private void ConnectToDevice_Click(object sender, RoutedEventArgs e) { this.EnableConnectionControls(false); this.EnableDeviceControls(false); this.ClearOutput(); portal = new DevicePortal( new DefaultDevicePortalConnection( this.address.Text, this.username.Text, this.password.Password)); StringBuilder sb = new StringBuilder(); Task connectTask = new Task( async() => { sb.Append(this.MarshalGetCommandOutput()); sb.AppendLine("Connecting..."); this.MarshalUpdateCommandOutput(sb.ToString()); portal.ConnectionStatus += (portal, connectArgs) => { if (connectArgs.Status == DeviceConnectionStatus.Connected) { sb.Append("Connected to: "); sb.AppendLine(portal.Address); sb.Append("OS version: "); sb.AppendLine(portal.OperatingSystemVersion); sb.Append("Device family: "); sb.AppendLine(portal.DeviceFamily); sb.Append("Platform: "); sb.AppendLine(String.Format("{0} ({1})", portal.PlatformName, portal.Platform.ToString())); } else if (connectArgs.Status == DeviceConnectionStatus.Failed) { sb.AppendLine("Failed to connect to the device."); sb.AppendLine(connectArgs.Message); } }; // TODO: Support proper certificate validation instead of blindly trusting this cert (Issue #154/#145). await portal.GetRootDeviceCertificate(true); await portal.Connect(); this.MarshalUpdateCommandOutput(sb.ToString()); }); Task continuationTask = connectTask.ContinueWith( (t) => { this.MarshalEnableDeviceControls(true); this.MarshalEnableConnectionControls(true); }); connectTask.Start(); }
private async Task <bool> AuthSilentAsync() { // Placeholder PasswordCredential cred = null; // Try to get user name and password try { cred = vault.FindAllByResource(PortalResourceName).FirstOrDefault(); // Password does not come across with the method above. We must call another method. if (cred != null) { cred = vault.Retrieve(PortalResourceName, cred.UserName); } } catch { } // If no credentials were found, fail if (cred == null) { return(false); } // Credentials found. Try and log into portal try { // Create portal object portal = new DevicePortal( new DefaultDevicePortalConnection( "https://127.0.0.1", cred.UserName, cred.Password)); // Get cert (OK to use untrusted since it's loopback) await portal.GetRootDeviceCertificate(acceptUntrustedCerts : true); // Attempt to connect await portal.Connect(); // Get IPD var ipd = await portal.GetInterPupilaryDistance(); // Success! return(true); } catch (Exception ex) { // Problem ShowError(ex.Message); return(false); } }
/// <summary> /// Click handler for the connectToDevice button. /// </summary> /// <param name="sender">The caller of this method.</param> /// <param name="e">The arguments associated with this event.</param> private void ConnectToDevice_Click(object sender, RoutedEventArgs e) { this.EnableConnectionControls(false); this.EnableDeviceControls(false); this.ClearOutput(); bool allowUntrusted = this.allowUntrustedCheckbox.IsChecked.Value; portal = new DevicePortal( new DefaultDevicePortalConnection( this.address.Text, this.username.Text, this.password.Password)); StringBuilder sb = new StringBuilder(); Task connectTask = new Task( async() => { sb.Append(this.MarshalGetCommandOutput()); sb.AppendLine("Connecting..."); this.MarshalUpdateCommandOutput(sb.ToString()); portal.ConnectionStatus += (portal, connectArgs) => { if (connectArgs.Status == DeviceConnectionStatus.Connected) { sb.Append("Connected to: "); sb.AppendLine(portal.Address); sb.Append("OS version: "); sb.AppendLine(portal.OperatingSystemVersion); sb.Append("Device family: "); sb.AppendLine(portal.DeviceFamily); sb.Append("Platform: "); sb.AppendLine(String.Format("{0} ({1})", portal.PlatformName, portal.Platform.ToString())); } else if (connectArgs.Status == DeviceConnectionStatus.Failed) { sb.AppendLine("Failed to connect to the device."); sb.AppendLine(connectArgs.Message); } }; try { // If the user wants to allow untrusted connections, make a call to GetRootDeviceCertificate // with acceptUntrustedCerts set to true. This will enable untrusted connections for the // remainder of this session. if (allowUntrusted) { await portal.GetRootDeviceCertificate(true); } await portal.Connect(manualCertificate: this.certificate); } catch (Exception exception) { sb.AppendLine(exception.Message); } this.MarshalUpdateCommandOutput(sb.ToString()); }); Task continuationTask = connectTask.ContinueWith( (t) => { this.MarshalEnableDeviceControls(true); this.MarshalEnableConnectionControls(true); }); connectTask.Start(); }