Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if PasswordVault/credential manager has app data at load

Hey I'm using PasswordVault for storing user credentials in my windows 8 app.

What I want the app to do on loading is check to see if the PasswordVault/credential manager already has a stored value for my app. if it don't don't I want it to stay on the page so the user can login, but if the credentials are already there then I would like it to go straight to page 2.

I tried using the following code:

private Windows.Security.Credentials.PasswordCredential GetCredentialFromLocker()
{
    Windows.Security.Credentials.PasswordCredential credential = null;

    var vault = new Windows.Security.Credentials.PasswordVault();
   
    var credentialList = vault.FindAllByResource("MYapp");
    if (credentialList.Count > 0)
        if (credentialList.Count == 1)
            credential = credentialList[0];
        else
            // User selecor
   
    return credential;
}

and then on page load I have

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    var loginCredential = GetCredentialFromLocker();

    if (loginCredential != null)
        this.Frame.Navigate(typeof(page2)); 
    else
    {
        loginBigButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
        signUpButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
        signUpTextBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
    }
}

The problem is that if there is no credential stored with the Resource (MYapp) the code:

var credentialList = vault.FindAllByResource("MYapp");

yields:

WinRT information: Cannot find credential in Vault

Additional information: Element not found.

like image 679
Kasper S Mathiesen Avatar asked Jul 30 '13 17:07

Kasper S Mathiesen


1 Answers

Method FindAllByResource throws exception when there are no credentials for specified resource, so you need to wrap it with try catch block.

Alternatively you can use 'RetrieveAll' which doesn't throw exception if there are no credentials stored and iterate over each returned PasswordCredential and check it's Resource property.

like image 105
Tadeusz Wójcik Avatar answered Oct 10 '22 22:10

Tadeusz Wójcik