Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move hello to foreground once window exists #27

Merged
merged 2 commits into from
Nov 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
nullpo-head marked this conversation as resolved.
Show resolved Hide resolved
{
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