4

Can I write a code block that opens the Windows Hello window when the button is clicked and takes action according to the correctness of the password? If I could write, how would I do it?

I'm working on: Windows Forms .NET Framework (C#)

1 Answers1

8

For a .NET Framework WinForms application, you need to follow these steps:

  1. Tools → NuGet Package Manager → Package Manager Settings → Make sure PackageReference is selected for Default package management format.
  2. Solution Explorer → Right click on your project → choose Manage NuGet Packages.
  3. In the Browse tab of the package manager, search for Microsoft.Windows.SDK.Contracts package. In the right pane of the NuGet Package Manager window select the desired version of the package based on the version of Windows 10 you want to target and click install.
  4. Then search for Microsoft.NETCore.UniversalWindowsPlatform package and install it.
  5. Add the following code in the application, for example in a button click handler:
    // Required using statements:
    // using Windows.Security.Credentials;
    private async void button1_Click(object sender, EventArgs e)
    {
        bool supported = await KeyCredentialManager.IsSupportedAsync();
        if (supported)
        {
            KeyCredentialRetrievalResult result =
                await KeyCredentialManager.RequestCreateAsync("login", 
                KeyCredentialCreationOption.ReplaceExisting);
            if (result.Status == KeyCredentialStatus.Success)
            {
                MessageBox.Show("Logged in.");
            }
            else
            {
                MessageBox.Show("Login failed.");
            }
        }
    }
    

Here is the result:

enter image description here

To learn more, take a look at the following docs:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I downloaded the packages you specified, but I am getting an error. I can't call them in my code as library. The definition is undefined in the code. 1) Name 'KeyCredentialManager' does not exist in current context 2) The type 'KeyCredentialRetrievalResult' or namespace name was not found (missing the usage directive or an assembly reference?) 3) Name 'KeyCredentialManager' does not exist in current context 4) Name 'KeyCredentialCreationOption' does not exist in current context 5) Name 'KeyCredentialStatus' does not exist in current context – Omer Huseyin GUL Apr 12 '22 at 23:11
  • I'm also using Windows 11. But many software that does not support Windows 11 and supports Windows 10 can run on my system. If I can solve this error maybe I can use it. – Omer Huseyin GUL Apr 12 '22 at 23:13
  • `using Windows.Security.Credentials;` – Reza Aghaei Apr 12 '22 at 23:52
  • I have tested the solution in Windows 10. – Reza Aghaei Apr 12 '22 at 23:53
  • I am using windows 11. But the library you mentioned does not exist. Should I download UWP components from Visual Studio 2022 Installer? – Omer Huseyin GUL Apr 12 '22 at 23:59
  • @laweis The libraries which I mentioned are NuGet packages. Please follow the steps carefully. The details which are mentioned in each step are important. For example if you do not complete step one, then you will have some build errors. – Reza Aghaei Apr 13 '22 at 00:01
  • You may need to edit the csproj, and set the net6.0-windows10.0.19041.0 – Fiach Reid Jan 15 '23 at 12:36
  • 1
    Thanks @FiachReid, True for .NET, but for .NET Framework (target of this question) you need proper version of `Microsoft.Windows.SDK.Contracts` package. It's explained with more details [here](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance?WT.mc_id=DT-MVP-5003235). Same approach has been used in my other answer [Showing emoji panel](https://stackoverflow.com/a/65429703/3110834) and [Using MediaCapture api](https://stackoverflow.com/a/74949797/3110834). – Reza Aghaei Jan 15 '23 at 13:49
  • Tried this with SDK7, and it refuses to build due to NETSDK1135: https://learn.microsoft.com/en-us/dotnet/core/tools/sdk-errors/netsdk1135 – Daniel Feb 02 '23 at 10:43
  • @Daniel The code is the same for .NET 7, but the steps are different. No need to the NuGet package, but change the TargetFramework like [this post](https://stackoverflow.com/a/75317921/3110834). It's mentioned in the links in my previous comment as well. – Reza Aghaei Feb 02 '23 at 11:03