SecureString.MakeReadOnly Method

Makes the text value of this secure string read-only.

Namespace:  System.Security
Assembly:  mscorlib (in mscorlib.dll)

public void MakeReadOnly()

ExceptionCondition
ObjectDisposedException

This secure string has already been disposed.

Initialize the text value of an instance of the SecureString class with the SecureString constructors, and modify the value with the Clear, RemoveAt, SetAt, InsertAt, and AppendChar methods.

After you have made your final modifications, use the MakeReadOnly method to make the value of the instance immutable (read-only). After the value is marked as read-only, any further attempt to modify it throws an InvalidOperationException.

The effect of invoking MakeReadOnly is permanent because no means is provided to make the secure string modifiable again. Use the IsReadOnly method to test whether an instance of SecureString is read-only.

The following code example demonstrates how the AppendChar and RemoveAt methods can be used to collect the characters in a password. After the password is collected, it is made read-only.

// This example demonstrates using the AppendChar and RemoveAt  
// methods to collect a password. 

using System;
using System.Security;

class Sample 
{
    public static void Main() 
    {
    ConsoleKeyInfo cki;
    String m1 = "(This example simulates entering a password. " +
                "Do not enter an actual password.)\n";
    String m2 = "Enter your password (up to 15 letters, numbers, and underscores)\n" +
                "Press BACKSPACE to delete the last character entered. \nPress ESCAPE to quit:";
    SecureString password = new SecureString();
    int top, left;

// The Console.TreatControlCAsInput property prevents this example from 
// ending if you press CTL+C, however all other operating system keys and  
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect.  
// Each input character is assumed to occupy one screen column.

    Console.TreatControlCAsInput = true;

    Console.Clear();
    Console.WriteLine(m1);
    Console.WriteLine(m2);

    top  = Console.CursorTop;
    left = Console.CursorLeft;

// Read user input from the console. Store up to 15 letter, digit, or underscore 
// characters in a SecureString object, or delete a character if the user enters  
// a backspace. Display an asterisk (*) on the console to represent each character  
// that is stored. 

    while (true)
        {
        cki = Console.ReadKey(true);
        if (cki.Key == ConsoleKey.Escape) break;
        if (cki.Key == ConsoleKey.Backspace)
            {
            if (password.Length > 0) 
                {
                Console.SetCursorPosition(left+password.Length-1, top);
                Console.Write(' ');
                Console.SetCursorPosition(left+password.Length-1, top);
                password.RemoveAt(password.Length-1);
                }
            }
        else
            {
            if ((password.Length < 15) && 
                (Char.IsLetterOrDigit(cki.KeyChar) || cki.KeyChar == '_'))
                {
                password.AppendChar(cki.KeyChar);                 
                Console.SetCursorPosition(left+password.Length-1, top);
                Console.Write('*');
                }
            }
        }

// Make the password read-only to prevent it from being modified after it has  
// been collected. 

    password.MakeReadOnly();
    }
}
/*
This example produces results similar to the following text:

(This example simulates entering a password. Do not enter an actual password.)

Enter your password (up to 15 letters, numbers, and underscores)
Press BACKSPACE to delete the last character entered.
Press ESCAPE to quit:
***************

*/

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft