检查PasswordVault /凭证管理器在加载时是否有应用数据 [英] Check if PasswordVault/credential manager has app data at load

查看:286
本文介绍了检查PasswordVault /凭证管理器在加载时是否有应用数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我使用PasswordVault用于存储用户凭证在我的Windows 8应用。



我想什么应用做加载检查,看是否PasswordVault /凭据管理器已经为我的应用程序的存储值。如果不这样做我不希望它留在页面上,使用户可以登录,但如果凭证已经存在那么我想它直接进入第2页。



我用下面的代码尝试:

 私人Windows.Security.Credentials.PasswordCredential GetCredentialFromLocker()
{
Windows.Security.Credentials.PasswordCredential凭证= NULL;

变种库=新Windows.Security.Credentials.PasswordVault();

VAR credentialList = vault.FindAllByResource(MYAPP);
如果(credentialList.Count大于0)
如果(credentialList.Count == 1)
凭证= credentialList [0];
,否则
//用户selecor

返回凭证;
}



然后在页面加载我

 私人无效PAGE_LOADED(对象发件人,RoutedEventArgs E)
{
VAR loginCredential = GetCredentialFromLocker();

如果(!loginCredential = NULL)
this.Frame.Navigate(typeof运算(第2页));
,否则
{
loginBigButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
signUpButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
signUpTextBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}



的问题是,如果没有存储与无凭证资源(MYAPP)的代码:

  VAR credentialList = vault.FindAllByResource(MYAPP); 



收益率:




WinRT的信息:在存储库中找不到凭证



更多信息:找不到元素



< DIV CLASS =h2_lin>解决方案

方法 FindAllByResource 抛出异常时,有指定的资源没有凭据,所以你需要使用<把它包code>尝试捕捉块。



另外,您可以使用RetrieveAll,这并不抛出异常,如果没有存储的凭据和遍历每个返回 PasswordCredential ,并检查它的资源属性。


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.

解决方案

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.

这篇关于检查PasswordVault /凭证管理器在加载时是否有应用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆