Security Credential Picker In UWP With XAML And C#

Credential Picker creates an asynchronous object that displays a dialog box of credentials to the user and collects the user's response. After that, the responses retrieved from Credential Picker are passed to the required APIs.


Reading this article, we will learn how to use Credential Picker in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing the UWP app.

  1. Windows 10 (Recommended)
  2. Microsoft Visual Studio 2017

Step 1

Open Visual Studio 2017 and go to Start -> New Project-> select Windows Universal (under Visual C#) -> Blank App (Universal Windows). Give it a suitable name (such as - UWPCredentialPicker) and click OK.


Step 2 

After choosing the target and minimum platform version that your UWP application will support, the project creates App.xaml and MainPage.xaml files.


Step 3

Add TextBlock control and change the name and text property for Title.
Add the following controls for giving a custom message in Credential Picker.
Add a Stackpanel and inside that, add TextBlock, TextBox, Button Controls. Then, change the Name and Text property. 

Add an event for the Launch button for launching the Credential Picker Dialogue box.


Step 4

Add the following controls for retrieving the user responses from Credential Picker. 

Add a Stack panel. Inside that, add two TextBlock controls and change the Name and Text properties for Username and Password.


Step 5

Now, add the following namespace and code in MainPage.xaml.cs.

  1. using Windows.Security.Credentials.UI;  
  2. private async void btnLaunch_Click(object sender, RoutedEventArgs e) {  
  3.     if ((Txtmsg.Text.Length != 0)) {  
  4.         CredentialPickerResults credPickerResults = await CredentialPicker.PickAsync("", Txtmsg.Text);  
  5.         tblUN.Text = "User Name : " + credPickerResults.CredentialUserName;  
  6.         tblPWD.Text = "Password : " + credPickerResults.CredentialPassword;  
  7.     }  
  8. }  

Note - Automatically, the following code will be generated in XAML code view while we are done in the design view.

  1. <Page x:Class="UWPCredentialPicker.MainPage" xmlns="" xmlns:x="" xmlns:local="using:UWPCredentialPicker" xmlns:d="" xmlns:mc="" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="TblTitle" Margin="59,39,58,0" TextWrapping="Wrap" Text="Credential Picker in UWP " VerticalAlignment="Top" Height="29" FontWeight="Bold" FontSize="20" />  
  4.         <StackPanel x:Name="SPcp" HorizontalAlignment="Left" Height="87" Margin="59,73,0,0" VerticalAlignment="Top" Width="243">  
  5.             <TextBlock x:Name="TblMsg" TextWrapping="Wrap" Text="Message" Height="23" Margin="0,0,165,0" />  
  6.             <TextBox x:Name="Txtmsg" TextWrapping="Wrap" Text="Enter Your Credential Here" />  
  7.             <Button x:Name="btnLaunch" Content="Launch" Click="btnLaunch_Click" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="45" /> </StackPanel>  
  8.         <StackPanel HorizontalAlignment="Left" Height="228" Margin="10,191,0,0" VerticalAlignment="Top" Width="340">  
  9.             <TextBlock x:Name="TblRes" Text="Retrived Data from Credential Picker " TextWrapping="Wrap" FontWeight="Bold" HorizontalAlignment="Center" />  
  10.             <TextBlock x:Name="tblUN" TextWrapping="Wrap" Text="User Name " />  
  11.             <TextBlock x:Name="tblPWD" Text="Password " /> </StackPanel>  
  12.     </Grid>  
  13. </Page>  
 Step 6

Deploy your app on local machine. The output of the UWPCredentialPicker App is shown below.


After clicking the Launch button -

After clicking the OK button in Credential Picker -


After changing the Message Information -


Now, you have successfully tested Credential Picker in Visual C# - UWP environment in Visual Studio 2017.