Skip to content

Instantly share code, notes, and snippets.

@smiley
Created January 11, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smiley/ff0e3ceee31a88f9c39745dc9f714e93 to your computer and use it in GitHub Desktop.
Save smiley/ff0e3ceee31a88f9c39745dc9f714e93 to your computer and use it in GitHub Desktop.
Basic Windows Hello example in C#
// Adapted from http://j4ni.com/blog/?p=269
using System;
using Windows.Security.Credentials;
using Windows.Security.Cryptography;
using System.Threading.Tasks;
namespace AuthExample
{
class Program
{
private static async Task<bool> authenticateUser()
{
// Do we have capability to provide credentials from the device
if (await KeyCredentialManager.IsSupportedAsync())
{
// Get credentials for current user and app
KeyCredentialRetrievalResult result = await KeyCredentialManager.OpenAsync("MyAppCredentials");
if (result.Credential != null)
{
KeyCredentialOperationResult signResult =
await result.Credential.RequestSignAsync(CryptographicBuffer.ConvertStringToBinary("LoginAuth",
BinaryStringEncoding.Utf8));
if (signResult.Status == KeyCredentialStatus.Success)
{
return true;
}
}
// No previous saved credentials found
else
{
KeyCredentialRetrievalResult creationResult =
await KeyCredentialManager.RequestCreateAsync("MyAppCredentials",
KeyCredentialCreationOption.ReplaceExisting);
if (creationResult.Status == KeyCredentialStatus.Success)
{
return true;
}
}
}
return false;
}
static void Main(string[] args)
{
var isAuthenticatedTask = authenticateUser();
isAuthenticatedTask.Wait();
var isAuthenticated = isAuthenticatedTask.Result;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B1009CEB-750D-4935-8E93-65EF3C7E5D40}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AuthExample</RootNamespace>
<AssemblyName>AuthExample</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<TargetPlatformVersion>10.0.14393.0</TargetPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>
<Reference Include="Windows">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd</HintPath>
</Reference>
<Reference Include="Windows.Foundation" />
<Reference Include="Windows.Foundation.FoundationContract">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Windows Kits\10\References\10.0.15063.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd</HintPath>
</Reference>
<Reference Include="Windows.Storage" />
</ItemGroup>
<ItemGroup>
<Compile Include="auth_example.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment