The passwordbox control is used to write small things today. After clicking a vertex next to the Instance name, I found that the text attribute (with the Password attribute) was not found. I found three methods to use passwordbox.
- Use the Password attribute value (this is the simplest)
- Use binding to associate the password value in passwordbox with a certain attribute of the Background Data Object
- Use the securepassword attribute of passwordbox
This article focuses on the third method. The securepassword attribute is added at. Net 3.5 SP1.
The data type of securepassword is securestring. for securepassword instances, it is easy to write values in and read values out. Otherwise, how is it called "encrypted string. note that if you call the tostring () method of a securepassword instance, the value you get will always be "system. security. securestring ".
The program running interface is as follows. You can guess the XAML Code. There is a passwordbox instance named passwordboxpassword.
The code for getting the password in passwordboxpassword is as follows:
// Use an intptr value to store the starting point of the encrypted string. <br/> intptr P = system. runtime. interopservices. marshal. securestringtobstr (this. passwordboxpassword. securepassword); </P> <p> // use. net internal algorithm converts the character set at which intptr points to a string <br/> string Password = system. runtime. interopservices. marshal. ptrtostringbstr (p); </P> <p> // verify it by the way <br/> If (string. isnullorempty (password) | password! = "123456") <br/>{< br/> MessageBox. show ("Enter Password", "prompt", messageboxbutton. OK, messageboximage. asterisk); <br/> return; <br/>}
If there are many passwordboxes in the program, we recommend that you reference system. runtime. interopservices namespace in advance.
Over