【WPF】自定义PassWordBox (可以绑定的) 、SecureString类型吗?
作者:互联网
目的
自定义一个可以绑定的密码输入框
知识点:自定义控件、 SecureString类型
System.Security.SecureString(表示应保密的文本) 保存非托管内存中,需要用指针逐个字符的读取。
正常的String类型值,在脱离开作用域之后,其值在内存中并不会被立即销毁,这时如果有人恶意扫描你的内存,程序中所保存的机密信息就会暴露;于是就有了System.Security.SecureString,SecureString表示一个应保密的文本,它在初始化时就已被加密,并且脱离作用域后会被立即销毁;
自定义用户控件
1、添加自定义用户控件类
2、xaml代码
<UserControl x:Class="Login.Resources.CustomeControls.PasswordBoxBingable" 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" xmlns:local="clr-namespace:Login.Resources.CustomeControls" mc:Ignorable="d" d:DesignHeight="40" d:DesignWidth="300"> <PasswordBox x:Name="Psb" VerticalContentAlignment="Center" Padding="10,0,0,0"/> </UserControl>
C#代码
using System; using System.Collections.Generic; using System.Linq; using System.Security; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Login.Resources.CustomeControls { /// <summary> /// BingablePassword.xaml 的交互逻辑 /// </summary> /// public partial class PasswordBoxBingable : UserControl { public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBingable)); public SecureString Password { get { return (SecureString)GetValue(PasswordProperty); } set { SetValue(PasswordProperty, value); } } public PasswordBoxBingable() { InitializeComponent(); Psb.PasswordChanged += OnPasswordChanged; } private void OnPasswordChanged(object sender, RoutedEventArgs e) { Password = Psb.SecurePassword; } } }
显示
应用自定义控件
标签:SecureString,控件,自定义,Windows,PassWordBox,System,using 来源: https://www.cnblogs.com/cdaniu/p/16655958.html