Skip to content

Commit

Permalink
feat: move hello to foreground once window exists (#27)
Browse files Browse the repository at this point in the history
* feat: move hello to foreground once window exists

* fix: assign focus task to discarded variable

prevents unnecessary compiler warning
  • Loading branch information
jonaskuske authored Nov 23, 2021
1 parent 85675eb commit b0b9f54
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.Security.Credentials;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using HWND = System.IntPtr;

namespace WindowsHelloAuthenticator
{
Expand Down Expand Up @@ -52,6 +55,28 @@ static string ExitCodeToMessage(int code, string key_name)
return "Unkwon internal error.";
}
}

[DllImport("user32.dll", SetLastError = true)]
static extern HWND FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll", SetLastError = true)]
static extern bool SetForegroundWindow(HWND hWnd);


static async Task<bool> FocusHelloWindow(CancellationToken token)
{
token.ThrowIfCancellationRequested();
HWND hwnd = FindWindow("Credential Dialog Xaml Host", null);

while ((int)hwnd == 0)
{
await Task.Delay(500);
hwnd = FindWindow("Credential Dialog Xaml Host", null);
token.ThrowIfCancellationRequested();
}

return SetForegroundWindow(hwnd);
}

static async Task<(int err, byte[] sig)> VerifyUser(string key_name, string contentToSign)
{
Expand All @@ -68,7 +93,14 @@ static string ExitCodeToMessage(int code, string key_name)
}

var buf = CryptographicBuffer.ConvertStringToBinary(contentToSign, BinaryStringEncoding.Utf8);

var tokenSource = new CancellationTokenSource();
_ = FocusHelloWindow(tokenSource.Token);

var signRes = await key.Credential.RequestSignAsync(buf);

tokenSource.Cancel();

if (signRes.Status != KeyCredentialStatus.Success)
{
return (CredentialStatusToExitCode(key.Status), null);
Expand Down

0 comments on commit b0b9f54

Please sign in to comment.