PasswordBox和MVVM

Mar*_*tus 18 c# security wpf mvvm passwordbox

我们有以下场景:

  1. 用户可以放置密码的MVVM用户界面(实际上是a PasswordBox)
  2. 应该做一些工作的服务器
  3. 服务器连接到某些需要验证的数据库

我已经在MVVM中的PasswordBox上阅读了这个问题

但是如何做却没有答案!只是"永远不会那样".

传递密码的正确方法是什么?如何解决安全问题?

有没有适当的方式BindingPasswordBox和密码不得保存在某个地方,好不好.

那么,MVVM做这些事情的方式是什么?

即使模式被破坏,有没有一种很好的方法来实现这样的事情?

想到Func<string>要检索它,但没有绑定这会弄得一团糟......

更新 相同用于从(希望加密的)密码存储区初始化PasswordBox.这不是打破MVVM模式吗?用户不希望每次启动应用程序时输入密码,或者想要使用我相信的数据库.

Rac*_*hel 38

就个人而言,我只是将整个PasswordBox控件传递给我的LoginCommand

我知道它打破了MVVM,因为ViewModel层现在引用了特定于View的对象,但我认为在这种特定情况下它可以.

所以我可能有这样的XAML:

<Button Content="Login" 
        Command="{Binding LoginCommand}" 
        CommandParameter="{Binding ElementName=MyPasswordBox}" />
Run Code Online (Sandbox Code Playgroud)

并且LoginCommand,做这样的事情:

private void Login(object obj)
{
    PasswordBox pwBox = obj as PasswordBox;

    SomeBlackBoxClass.ValidatePassword(UserName, pwBox.Password);
}
Run Code Online (Sandbox Code Playgroud)

我想你也可以在值上运行某种加密算法,并将该值的哈希值与用户密码的哈希值进行比较

private void Login(object obj)
{
    PasswordBox pwBox = obj as PasswordBox;
    var encryptedPassword = SomeLibrary.EncryptValue(pwBox.Password, someKey);

    if (encryptedPassword == User.EncryptedPassword)
        // Success
}
Run Code Online (Sandbox Code Playgroud)

我不是PasswordBox控制或安全方面的专家,但我知道您不希望在应用程序的内存中的任何位置以明文形式存储用户的密码

(从技术上讲,它以纯文本形式存储PasswordBox.Password- 你可以使用像Snoop这样的东西来验证这一点 - 但是通常PasswordBox的存在时间不会超过用户登录的时间,而实际的"密码"只是文本由用户输入,可能是也可能不正确.键盘记录器可以为您提供相同的信息.)


小智 5

我已经通过创建一个UserControl来解决此问题,该UserControl公开了可以绑定的SecureString依赖项属性。此方法始终将密码保留在SecureString中,并且不会“破坏” MVVM。

用户控件

XAML

<UserControl x:Class="Example.PasswordUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>       
        <PasswordBox Name="PasswordBox" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

CS

public partial class PasswordUserControl : UserControl
{
    public SecureString Password
    {
        get { return (SecureString) GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(SecureString), typeof(UserCredentialsInputControl),
            new PropertyMetadata(default(SecureString)));


    public PasswordUserControl()
    {
        InitializeComponent();

        // Update DependencyProperty whenever the password changes
        PasswordBox.PasswordChanged += (sender, args) => {
            Password = ((PasswordBox) sender).SecurePassword;
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

用法示例

使用控件非常简单,只需将控件上的密码DependencyProperty绑定到ViewModel上的Password属性即可。ViewModel的Password属性应为SecureString。

<controls:PasswordUserControl Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)

将绑定上的Mode和UpdateSource触发器更改为最适合您的设置。

如果您需要纯文本密码,则以下页面介绍在SecureString和字符串之间进行转换的正确方法:http : //blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-properly -convert-securestring-to-string.aspx。当然,您不应该存储纯文本字符串...