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.