Simple usage of passwordbox in WPF

Source: Internet
Author: User

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.

  1. Use the Password attribute value (this is the simplest)
  2. Use binding to associate the password value in passwordbox with a certain attribute of the Background Data Object
  3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.