7

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.

1
  • I wonder if anyone knows the size limit of the password field? Nov 16, 2020 at 18:19

2 Answers 2

4

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.

5
  • thanks for the quick reply mate,,, I tried putting a try and catch around it already, as one of the first things, but it still throws the exception .. Using RetrieveAll how would I find the Resource property it cant seen to get it to show.. Jul 30, 2013 at 20:05
  • strange, it should catch that exception, can you update your code with try catch ? Also check social.msdn.microsoft.com/Forums/windowsapps/en-US/…
    – T W
    Jul 30, 2013 at 20:27
  • Try using catch(Exception e) instead of specifying the exception type
    – sLedgem
    Jul 31, 2013 at 5:11
  • I'm also getting an exception despite using a try/catch. Did you manage to find a solution?
    – Gordonium
    Jul 3, 2015 at 19:16
  • I'm having an issue with vault.FindAllByResource("MYapp").FirstOrDefault(); that sometimes from some PCs even if the resource exists it throws an exception mentioning that resource not exist. Is that possible? What could I do to prevent it? Jun 23, 2018 at 6:28
3

I'll try to answer this question the best as I can:

Firstable as Tadeusz said FindAllByResource throws an exception when there are no credentials for specified resource, in order to dont crash you app, you would need to wrap you logic within a try-catch block.

the reason to do this is for you to be able to create your resource in case you dont have one. so the way I see it, you logic should look like this:

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

    var vault = new Windows.Security.Credentials.PasswordVault();

    try
    {
       var credential = vault.FindAllByResource("MYapp").FirstOrDefault();

       return credential;
    }
    catch(Exception ex)
    {
       Debug.WriteLine($"Error retrieving Token: {ex.Message}");
    }
    return null;
}

now all you have to do is to store a new token in your passwordvault

you have to login first and then you store the token, so next time you try to retrieve your credentials it wont throw an exception since its already store. by example it should be something like this:

await client.LoginAsync(provider);

then you store your token like this:

PasswordVault.Add(new PasswordCredential("MYapp", 
                  client.CurrentUser.UserId, 
                  client.CurrentUser.MobileServiceAuthenticationToken));

I hope this answer helps in order, sorry for the late answer but, I found myself trying to solve this problem right now and I thought i should give a more detail or complete answer to this question.

Also you should consider token expiration checks and refreshing handling.

1
  • I'm having an issue with vault.FindAllByResource("MYapp").FirstOrDefault(); that sometimes from some PCs even if the resource exists it throws an exception mentioning that resource not exist. Is that possible? What could I do to prevent it? Jun 23, 2018 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.