public void ActivateScreen() { var lockState = WorkstationHelper.GetCurrentSessionLockState(); if (lockState == WorkstationHelper.LockState.Locked || lockState == WorkstationHelper.LockState.Unknown) { try { // Should trigger activation of the screen in credential provider with zero impact on user //inputSimulator.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.F12); inputSimulator.Mouse.RightButtonClick(); // Windows 10 lockui ignores right mouse button for controls interaction } catch (Exception ex) { // The exception generated by library uses generic Exception class if (ex.Message.StartsWith("Some simulated input commands were not sent successfully")) { // This exception is thrown when the library could not successfully send simulated input // To the target window, usually due to User Interface Privacy Isolation (UIPI) _log.WriteLine("UIPI prevented simulated input", LogErrorSeverity.Warning); } else { throw; } } } }
/// <summary> /// Create and authorize remote device, then load credentials storage from this device /// </summary> /// <param name="authorizeDevice">If false, skip remote device authorization step. Default is true.</param> public async Task InitRemoteAndLoadStorageAsync(bool authorizeDevice = true) { if (Interlocked.CompareExchange(ref _interlockedRemote, 1, 0) == 0) { try { if (!IsCreatingRemoteDevice && !IsAuthorizingRemoteDevice && !IsLoadingStorage && WorkstationHelper.GetCurrentSessionLockState() == WorkstationHelper.LockState.Unlocked) { try { authCancellationTokenSource = new CancellationTokenSource(); var ct = authCancellationTokenSource.Token; remoteCancellationTokenSource = new CancellationTokenSource(); _infNid = Guid.NewGuid().ToString(); _errNid = Guid.NewGuid().ToString(); await CreateRemoteDeviceAsync(remoteCancellationTokenSource.Token); if (remoteCancellationTokenSource.IsCancellationRequested) { _log.WriteLine($"({SerialNo}) Remote vault creation cancelled"); return; } if (_remoteDevice != null) { _log.WriteLine($"({_remoteDevice.SerialNo}) Remote vault created"); await _remoteDevice.RefreshDeviceInfo(); if (_remoteDevice.AccessLevel != null) { _log.WriteLine($"({_remoteDevice.SerialNo}) access profile (allOk:{_remoteDevice.AccessLevel.IsAllOk}; " + $"pin:{_remoteDevice.AccessLevel.IsPinRequired}; " + $"newPin:{_remoteDevice.AccessLevel.IsNewPinRequired}; " + $"button:{_remoteDevice.AccessLevel.IsButtonRequired}; " + $"link:{_remoteDevice.AccessLevel.IsLinkRequired}; " + $"master:{_remoteDevice.AccessLevel.IsMasterKeyRequired}; " + $"locked:{_remoteDevice.AccessLevel.IsLocked})"); } else { _log.WriteLine($"({_remoteDevice.SerialNo}) access level is null"); } } if (_remoteDevice.IsLockedByCode) { throw new HideezException(HideezErrorCode.DeviceIsLocked); } else if (authorizeDevice && !IsAuthorized && _remoteDevice?.AccessLevel != null && !_remoteDevice.AccessLevel.IsAllOk) { await AuthorizeRemoteDevice(ct); } else if (!authorizeDevice && !IsAuthorized && _remoteDevice?.AccessLevel != null && _remoteDevice.AccessLevel.IsAllOk) { await AuthorizeRemoteDevice(ct); } else if (_remoteDevice?.AccessLevel != null && _remoteDevice.AccessLevel.IsLocked) { throw new HideezException(HideezErrorCode.DeviceIsLocked); } if (!ct.IsCancellationRequested && !IsStorageLoaded) { await LoadStorage(); } } catch (HideezException ex) when(ex.ErrorCode == HideezErrorCode.DeviceDisconnected) { _log.WriteLine("Remote vault creation aborted, vault disconnected", LogErrorSeverity.Warning); } catch (HideezException ex) when(ex.ErrorCode == HideezErrorCode.DeviceIsLocked) { if (_remoteDevice.IsLockedByCode) { await _metaMessenger.Publish(new ShowLockNotificationMessage(TranslationSource.Instance["Notification.DeviceLockedByCode.Message"], TranslationSource.Instance["Notification.DeviceLockedByCode.Caption"], new NotificationOptions() { CloseTimeout = NotificationOptions.LongTimeout }, Mac)); } else if (_remoteDevice.IsLockedByPin) { await _metaMessenger.Publish(new ShowLockNotificationMessage(TranslationSource.Instance["Notification.DeviceLockedByPin.Message"], TranslationSource.Instance["Notification.DeviceLockedByPin.Caption"], new NotificationOptions() { CloseTimeout = NotificationOptions.LongTimeout }, Mac)); } else { _log.WriteLine(ex); } } catch (HideezException ex) when(ex.ErrorCode == HideezErrorCode.DeviceIsLockedByPin) { } catch (HideezException ex) when(ex.ErrorCode == HideezErrorCode.DeviceIsLockedByCode) { } catch (Exception ex) { ShowError(ex.Message, _errNid); } finally { var tmp = authCancellationTokenSource; authCancellationTokenSource = null; tmp.Dispose(); tmp = remoteCancellationTokenSource; remoteCancellationTokenSource = null; tmp.Dispose(); } } } finally { Interlocked.Exchange(ref _interlockedRemote, 0); } } }