Store User Login Credentials Securely in Windows Store App

Today we are talking about how to save your username and password credentials securely in your Metro style app. In a Metro Style app you can use the PasswordVault Class to store credentials for your app. The PasswordVault Class represents a Credential Locker of credentials. This class can be used with C#, C++ and also with VB code behind languages. But in this article we use with C# code.

The PasswordVault Class is a sealed class and inherited from the object class. This class cannot be inherited by other classes.

public sealed class PasswordVault

The following methods can be used with the PasswordVault Class:

  • Add: This method Adds a credential to the Credential Locker. It takes the object of PasswordCredential class as an argument that represents the Credential Locker.
  • FindAllByResource: This method helps you to search the credentials from the locker. It takes a resource as an argument. It returns a list of objects of the PasswordCredential class of the matching items.
  • FindAllByUserName: It searches the Credential Locker for credentials from the locker. It takes the user name to match the credential.
  • Remove: This method is used to remove a credential from the Credential Locker. It takes one argument, the object of the PasswordCredential Class.
  • Retrieve: This method is used to retrieve a set of credentials from the Credential Locker.
  • RetrieveAll: It retrieves every set of credentials stored in the Credential Locker.

In order to use the PasswordVault Class we first learn about the PasswordCredential Class. The PasswordCredential Represents the password credential store:

  • The PasswordCredential class has one method.
  • RetrievePassword: Populates the password for the credential.
  • The PasswordCredential class has these properties.
  • Password: Gets or sets the password string of the credential.
  • Resource: Gets or sets the resource of the credential.
  • UserName: Gets or sets the user name of the credential.

Now, we design a page to see how to use these classes to store user credenitals.

Here is the Code of the XAML file:

<Page

    x:Class="passwordvalult.MainPage"

    IsTabStop="false"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:passwordvalult"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

<TextBlock HorizontalAlignment="Left" Margin="147,49,0,0" TextWrapping="Wrap" Text="User Name" VerticalAlignment="Top" Style="{StaticResource BasicTextStyle}"/>

        <TextBox x:Name="user" TextWrapping="Wrap" Width="300" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="233,43,0,0" Grid.Row="1"/>

        <TextBlock  HorizontalAlignment="Left" Margin="158,100,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top" Style="{StaticResource BasicTextStyle}" />

        <TextBox x:Name="pwd" TextWrapping="Wrap" Width="300" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="233,94,0,0" Grid.Row="1"/>

        <Button x:Name="login" Content="Store"  HorizontalAlignment="Left" Margin="233,144,0,0" VerticalAlignment="Top" Click="Button_Click_1" Style="{StaticResource TextButtonStyle}"/>

       <Button x:Name="cancel" Content="Reterive" HorizontalAlignment="Left" Margin="400,144,0,0"  VerticalAlignment="Top" Click="Button_Click_2" Style="{StaticResource TextButtonStyle}"/>

    </Grid>

</Page>


In the preceding code we create two textboxes and two buttons. One button is to save user credentials in the PasswordValut class and the other is used to retrieve credentials from the class.

Here is the code of XAML.cs file:
 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using Windows.Security.Credentials;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

 

namespace passwordvalult

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        public static PasswordVault locker = new PasswordVault();

        PasswordCredential creadential = new PasswordCredential();

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            if (user.Text == "gaurav" && pwd.Text == "delhi")

            {

                creadential.Password = pwd.Text;

                creadential.Resource = "Login";

                creadential.UserName = user.Text;

                locker.Add(creadential);            

            }

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)

        {

          creadential=  locker.Retrieve("Login", user.Text);        

        }

    }

}


We can also use other methods of the PasswordValult according to the needs I explained above.

Summary: In this article we learn how to save user credentials to the PasswordVault class using the PasswordCredential class. It is secure and the best to store user Credentials.

Up Next
    Ebook Download
    View all
    Learn
    View all