我有一个使用UseSystemPasswordChar的文本框,因此它不会显示用户输入的密码。问题是密码仍然可以被类似Spy ++的东西读取。我正在寻找一种方法来隐藏它,就像他们在Services.msc>中的密码字段中那样。登录选项卡。
答案 0 :(得分:2)
这是我到目前为止所做的。
你可以通过一些独特的事件来表明是否已经接受了按下的键,是否已经改变了InputFilter或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;
}
}
}
答案 1 :(得分:1)
所以尝试这样的事情
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 += "*";
}
答案 2 :(得分:1)
如果您打算收集Windows /域用户凭据,则不应使用自己的对话框。您应该使用Windows通过PInvoke提供的内容,或者只使用这样的包装器,
http://weblogs.asp.net/hernandl/archive/2005/11/21/usercredentialsdialog.aspx
这个,