首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置PasswordBox的初始值

设置PasswordBox的初始值
EN

Stack Overflow用户
提问于 2015-08-19 07:35:04
回答 2查看 8K关注 0票数 3

我想知道,对于PasswordBox控件所涉及的所有安全性,这是否是可能的:

我有一个XAML表单(C#/WPF),用户将在其中配置数据库访问。在这种形式下,我使用一个PasswordBox来获取Server用户密码。

由于这些数据保存到磁盘以供将来使用(在pasword保护的Server数据库文件中),在第一次运行时没有设置密码,如果用户返回并需要编辑SQL连接,那么可能会有一个密码保存在以前的配置中(除非他使用Windows身份验证而不是SQL用户身份验证)

因此,我希望在第一次运行时显示一个空的PasswordBox,但是如果已经设置了密码,当用户返回时,我希望显示X号'*‘(指示密码已经到位。

因为PasswordBox.Password是不可绑定的,所以我只能选择总是显示它为空,或者总是显示一个固定数目的“*”(通过设置一个默认密码,这个密码实际上并不代表真正的密码)。

有没有其他的选择(当然,除了注入绑定的PasswordBox帮助程序-我不想走这条路,因为我可能没有考虑到MS选择不将它绑定到SecureString上)?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-20 01:29:24

您可以从文件中读取密码。

代码语言:javascript
运行
复制
//Storing the Password in String.
string pwd = "Password Read from the file";
PasswordBox.Password = pwd;

因此,当应用程序第一次打开并且文件中没有任何密码时,它将显示空的PasswordBox。同样,当用户已经设置了密码时,将在文件中找到密码,并将其加载到PasswordBox中。

票数 5
EN

Stack Overflow用户

发布于 2015-08-19 08:18:40

您可以为PasswordBox提供此行为,以便在MVVM中启用绑定。

PasswordBoxBehavior.cs

代码语言:javascript
运行
复制
public class PasswordBoxBehavior : Behavior<PasswordBox>
{
    public bool ResetPassword
    {
        get { return (bool)GetValue(ResetPasswordProperty); }
        set { SetValue(ResetPasswordProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ResetPassword.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ResetPasswordProperty =
        DependencyProperty.Register("ResetPassword", typeof(bool), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnResetPasswordChanged));

    static void OnResetPasswordChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
        PasswordBox item = behavior.AssociatedObject as PasswordBox;
        if (item == null)
            return;

        if ((bool)e.NewValue)
            item.Password = string.Empty;

        behavior.ResetPassword = false;
    }

    private bool isRoutedEventHandlerAssign;
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged));

    static void OnTextChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
        PasswordBox item = behavior.AssociatedObject as PasswordBox;
        if (item == null)
            return;

        if (item.Password != e.NewValue as string)
        {
            item.Password = e.NewValue as string;
        }

        if (!behavior.isRoutedEventHandlerAssign)
        {
            item.PasswordChanged += (sender, eArg) =>
            {
                behavior.Text = item.Password;
            };
            behavior.isRoutedEventHandlerAssign = true;
        }
    }

    public PasswordBoxBehavior()
    {
    }
}

使用

代码语言:javascript
运行
复制
<PasswordBox>
    <i:Interaction.Behaviors>
        <bh:PasswordBoxBehavior 
            Text="{Binding UserPassword}"
            ResetPassword="{Binding IsResetPassword}" />
    </i:Interaction.Behaviors>
</PasswordBox>

哪里

代码语言:javascript
运行
复制
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bh="clr-namespace:<some namespace>;assembly=<some assembly>"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32089243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档