daniele3004
daniele3004

Reputation: 13900

How to remove TextBox with password from the memory?

I have simple Login form.

After the user have digit password I need to destroy textbox to the memory.

Is it enough to do this?

txtPassword.Text = string.Empty;
txtPassword = null;
System.GC.Collect(); 

Upvotes: 0

Views: 727

Answers (2)

Alieza
Alieza

Reputation: 1

Set it into:

txtPassword.PasswordChar = '\0';

Upvotes: 0

JanMer
JanMer

Reputation: 1266

I think you might be interested in SecureString

An instance of the System.String class is both immutable and, when no longer needed, cannot be programmatically scheduled for garbage collection; that is, the instance is read-only after it is created, and it is not possible to predict when the instance will be deleted from computer memory. Because System.String instances are immutable, operations that appear to modify an existing instance actually create a copy of it to manipulate. Consequently, if a String object contains sensitive information such as a password, credit card number, or personal data, there is a risk the information could be revealed after it is used because your application cannot delete the data from computer memory.

A SecureString object is similar to a String object in that it has a text value. However, the value of a SecureString object is pinned in memory, may use a protection mechanism, such as encryption, provided by the underlying operating system, can be modified until your application marks it as read-only, and can be deleted from computer memory either by your application calling the Dispose method or by the .NET Framework garbage collector.

Upvotes: 3

Related Questions