PasswordBox.SecurePassword在WinRT Store App上不可用

时间:2014-10-05 07:56:23

标签: c# wpf windows-runtime

我正在为Windows 8.1,C#和.NET Framework 4.5.1开发Windows应用商店应用。

我尝试使用PasswordBox.SecurePassword,但似乎无法使用。我已经将框架版本更改为4.5,我已尝试使用.NET Framework 4.5.1的WPF测试项目,但它不可用。

我尝试将PresentationFramework.dll程序集添加到我的Windows 8.1商店应用程序中,但我无法。

我试图这样做:

{
    PasswordBox pass = new PasswordBox();
    pass.SecurePassword // I'm checking if it is available
}

要检查SecurePassword是否存在,但Windows应用商店应用中无法使用SecurePassword

Windows Store应用程序中是否有SecurePassword

1 个答案:

答案 0 :(得分:0)

我害怕说Windows Store应用程序无法使用SecureString
您应该使用Windows.Security.Credentials的方法和对象

10/07晚(日本时间)重写了 我希望你喜欢登录工作,不要这样做? 如果“Windows.Security.Credentials命名空间”是一个简单的解决方案。

请看上面的代码

cs文件

//MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;

namespace App1
{

public sealed partial class MainPage : Page
{
    PasswordVault vault = new PasswordVault();

    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;

        //Initialize Credential data
        try
        {
            var alldata = vault.FindAllByResource("My List");
            foreach (PasswordCredential data in alldata)
            {
                vault.Remove(data);
            }
        }
        catch { }
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //Certificate for Input data
        PasswordCredential credential = null;
        try
        {
            credential = vault.Retrieve("My List", Username.Text);

            if(credential.Password==password.Password)             
            {
                Status.Text = "Status : Succeed in Certificating";
            }
            else
            {
                Status.Text = "Status : Failed to Certification";
            }
        }
        catch { Status.Text = "Status : Failed to Certification"; }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        //Registar on Input data
        try
        {
            vault.Add(new PasswordCredential("My List", Username.Text, password.Password));
            Status.Text = "Status : Succeed in Registering";
        }
        catch
        {
            Status.Text = "Status : Failed to Register";
        }
    }
}
}

xaml文件

<Page>

    <!-- MainPage.xaml -->
    <Grid>
        <PasswordBox Name="password"  HorizontalAlignment="Left" Height="118" Margin="531,197,0,0" VerticalAlignment="Top" Width="464" FontSize="52"/>
        <TextBox Name="Username" HorizontalAlignment="Left" Height="118" Margin="531,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="464" FontSize="52"/>
        <TextBlock HorizontalAlignment="Left" Height="118" Margin="10,44,0,0" TextWrapping="Wrap" Text="UserName" VerticalAlignment="Top" Width="505" FontSize="80"/>
        <TextBlock HorizontalAlignment="Left" Height="120" Margin="10,195,0,0" TextWrapping="Wrap" Text="PassWord" VerticalAlignment="Top" Width="505" FontSize="80"/>
        <TextBlock Name="Status" HorizontalAlignment="Left" Height="111" Margin="10,327,0,0" TextWrapping="Wrap" Text="Status " VerticalAlignment="Top" Width="1329" FontSize="60"/>

        <Button Content="Certification" HorizontalAlignment="Left" Height="124" Margin="1006,41,0,0" VerticalAlignment="Top" Width="336" Click="Button_Click" FontSize="52"/>

        <Button Content="Registration" HorizontalAlignment="Left" Height="124" Margin="1006,194,0,0" VerticalAlignment="Top" Width="336" Click="Button_Click_1" FontSize="52"/>

    </Grid>
</Page>

1.制作“PasswordVault”实例

- 记录数据
2a。使用PasswordCredential实例的PasswordVault.Add方法 PasswordCredential Instance需要3个参数
第一个参数:资源名称(任意值)类型字符串
第二个参数:UserName类型字符串
第3个参数:密码类型字符串

- 证明数据
2b。使用PasswordVault.Retribe方法与2paraeters
第一个参数:资源名称(您必须使用您在注册时使用的相同名称)
第二个参数:用户名
return PasswordCredential value
您可以从PasswordCredential值获取密码和用户名

例如

//data is a PasswordCredential value

//get Username
string username = data.UserName;
//get Password
string password = data.Password