我们有以下情况:
PasswordBox)我已经读过这个MVVM中的PasswordBox问题了
但是,对于如何做却没有答案!只是多了“永远不要那么做”。
传递密码的正确方法是什么?如何解决安全问题?
没有正确的方式Binding到PasswordBox和密码不应存储在某处,好的。
那么,MVVM做这些事情的方式是什么呢?
即使模式被打破了,是否有一个好的方法来实现这些事情呢?
想要一个Func<string>来检索它,但是如果没有绑定,就会变得一团糟.
更新同样用于从(希望加密的)密码存储区初始化PasswordBox。这不是打破了MVVM模式吗?用户不希望每次启动应用程序时输入密码,也不希望使用我相信的数据库。
发布于 2013-03-13 16:41:40
就我个人而言,我只是将整个PasswordBox控件传递给我的LoginCommand
我知道它破坏了MVVM,因为ViewModel层现在引用了一个特定于视图的对象,但我认为在这个特定的情况下,它是可以的。
所以我可能有这样的XAML:
<Button Content="Login"
Command="{Binding LoginCommand}"
CommandParameter="{Binding ElementName=MyPasswordBox}" />和一个做这样的事情的LoginCommand:
private void Login(object obj)
{
PasswordBox pwBox = obj as PasswordBox;
SomeBlackBoxClass.ValidatePassword(UserName, pwBox.Password);
}我认为您还可以对值运行某种加密算法,并将该值的哈希与用户密码的哈希进行比较。
private void Login(object obj)
{
PasswordBox pwBox = obj as PasswordBox;
var encryptedPassword = SomeLibrary.EncryptValue(pwBox.Password, someKey);
if (encryptedPassword == User.EncryptedPassword)
// Success
}我不是PasswordBox控件或安全性方面的专家,但我知道您不希望将用户的密码存储在应用程序内存中的任何地方的纯文本中
(从技术上讲,它以纯文本的形式存储在PasswordBox.Password中--如果需要,可以使用类似于斯诺普的内容来验证这一点--但通常PasswordBox的存在时间不会超过用户登录所需的时间,而实际的“密码”只是用户输入的文本,这可能是正确的,也可能是不正确的。键盘记录器可以给你提供同样的信息。)
发布于 2015-11-23 23:32:53
通过创建一个公开可以绑定到的UserControl依赖项属性的SecureString,我解决了这个问题。此方法在任何时候都将密码保存在SecureString中,并且不会“破坏”MVVM。
UserControl
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>政务司司长
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;
};
}
}示例用法
使用该控件非常简单,只需将控件上的密码DependencyProperty绑定到ViewModel上的密码属性即可。ViewModel的密码属性应该是一个SecureString。
<controls:PasswordUserControl Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>将绑定上的模式和UpdateSource触发器更改为对您最合适的。
如果您需要纯文本中的密码,下面的页面描述了在SecureString和string之间转换的正确方法:http://blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-properly-convert-securestring-to-string.aspx。当然你不应该存储纯文本字符串..。
发布于 2013-03-20 10:03:10
取决于您对mvvm的理解(在某些情况下,允许以我的方式编写代码)
所以我创建了一个PasswordBox和一个名为TextBlock的
Xaml
<PasswordBox Height="23" Width="156" PasswordChar="*" PasswordChanged="pwBoxUser_PasswordChanged"/>
<TextBlock Height="1" Width="1" Name="MD5pw" Text="{Binding Passwort, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}" VerticalAlignment="Top" />码后
private void pwBoxUser_PasswordChanged(object sender, RoutedEventArgs e)
{
var pBox =sender as PasswordBox;
string blank=pBox.Password;
//to crypt my blank Password
var sMD5 = myMD5.toMD5(blank); //implement your crypt logic here
blank ="";
MD5pw.Text = sMD5;
}就像你可以看到你的密码是保存的,你可以很容易地绑定到它。
https://stackoverflow.com/questions/15390727
复制相似问题