隐藏密码文本

本文关键字:文本 密码 隐藏 | 更新日期: 2023-09-27 18:33:04

我有一个使用UseSystemPasswordChar的文本框,因此它不会显示用户输入的密码。问题是密码仍然能够被类似Spy++的东西读取。我正在寻找一种方法来隐藏它,就像他们在 Services.msc>登录选项卡的密码字段中所做的那样。

隐藏密码文本

这是我到目前为止得到的。

您可以通过一些独特的事件来指示是否已接受按下的键,是否更改了输入过滤器或 RealText,等等来改善这一点......

另一个需要改进的好东西是 InputFilter 的默认用法,因为使用 char 和 Keys 实际上并不适用于许多特殊键。例如 - 目前,如果您在密码框处于焦点时按 Alt+F4,它将输入"s"...所以有一袋错误需要修复。

最后,可能有一种比我在那里所做的更优雅的方式来处理大写字母与非大写字母输入。

所以这里是:

public class PasswordBox : TextBox
{
    private string _realText;
    public string RealText
    {
        get { return this._realText; }
        set
        {
            var i = this.SelectionStart;
            this._realText = value ?? "";
            this.Text = "";
            this.Text = new string('*', this._realText.Length);
            this.SelectionStart = i > this.Text.Length ? this.Text.Length : i;
        }
    }
    private Func<KeyEventArgs, bool> _inputFilter;
    public Func<KeyEventArgs, bool> InputFilter
    {
        get { return this._inputFilter; }
        set { this._inputFilter = value ?? (e => true); }
    }
    public PasswordBox()
    {
        this.RealText = "";
        this.InputFilter = e => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".Any(c => c == e.KeyValue);
    }
    protected override void OnKeyDown(KeyEventArgs e)
    {
        e.SuppressKeyPress = true;
        switch (e.KeyCode)
        {
            case Keys.Back:
                if (this.SelectionStart > 0 || this.SelectionLength > 0)
                {
                    this.RealText = this.SelectionLength == 0
                                        ? this.RealText.Remove(--this.SelectionStart, 1)
                                        : this.RealText.Remove(this.SelectionStart, this.SelectionLength);
                }
                break;
            case Keys.Delete:
                if (this.SelectionStart == this.TextLength)
                {
                    return;
                }
                this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength == 0 ? 1 : this.SelectionLength);
                break;
            case Keys.X:
            case Keys.C:
            case Keys.V:
                if (e.Control)
                {
                    return;
                }
                goto default;
            case Keys.Right:
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Shift:
            case Keys.Home:
            case Keys.End:
                e.SuppressKeyPress = false;
                base.OnKeyDown(e);
                break;
            default:
                if (e.Control)
                {
                    e.SuppressKeyPress = false;
                    base.OnKeyDown(e);
                    break;
                }
                if (this.InputFilter(e))
                {
                    var c = (char)e.KeyValue;
                    if (e.Shift == IsKeyLocked(Keys.CapsLock))
                    {
                        c = char.ToLower(c);
                    }
                    this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength)
                        .Insert(this.SelectionStart, c.ToString());
                    this.SelectionStart++;
                }
                break;
        }
    }
}

所以试试这样的东西

        private string realpass = "";
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char) Keys.Back)
                realpass += realpass.Substring(0, realpass.Length - 2);
            realpass += e.KeyChar.ToString();
            textBox1.Text = "";
            for (int i = 0; i < realpass.Length; i++)
                textBox1.Text += "*";
        }

如果您打算收集 Windows/域用户凭据,则不应使用自己的对话框。你应该使用Windows通过PInvoke提供的东西,或者简单地使用这样的包装器,

http://weblogs.asp.net/hernandl/archive/2005/11/21/usercredentialsdialog.aspx

而这个,

http://credentials.codeplex.com/