SecureString.MakeReadOnly 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此安全字符串的文本值设置为只读。
public:
 void MakeReadOnly();public void MakeReadOnly();member this.MakeReadOnly : unit -> unitPublic Sub MakeReadOnly ()例外
已处置此安全字符串。
示例
以下示例演示如何 AppendChar 使用 和 RemoveAt 方法收集密码中的字符。 收集密码后,该密码将设为只读。
using System;
using System.Security;
class Example
{
   public static void Main()
   {
      ConsoleKeyInfo cki;
      String m = "\nEnter your password (up to 15 letters, numbers, and underscores)\n" +
                 "Press BACKSPACE to delete the last character entered. " +
                 "\nPress Enter when done, or ESCAPE to quit:";
      SecureString password = new SecureString();
      int top, left;
      // The Console.TreatControlCAsInput property prevents CTRL+C from
      // ending this example.
      Console.TreatControlCAsInput = true;
      Console.Clear();
      Console.WriteLine(m);
      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.
      do {
         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('*');
            }
         }
      } while (cki.Key != ConsoleKey.Enter & password.Length < 15);
      // Make the password read-only to prevent modification.
      password.MakeReadOnly();
      // Dispose of the SecureString instance.
      password.Dispose();
   }
}
// This example displays output like the following:
//    Enter your password (up to 15 letters, numbers, and underscores)
//    Press BACKSPACE to delete the last character entered.
//    Press Enter when done, or ESCAPE to quit:
//    ************
Imports System.Security
Class ExampleClass
   Public Shared Sub Main()
      Dim cki As ConsoleKeyInfo
      Dim m As String = vbCrLf & "Enter your password (up to 15 letters, numbers, and underscores)" &
                        vbCrLf & "Press BACKSPACE to delete the last character entered. " & vbCrLf &
                        "Press Enter when done, or ESCAPE to quit: "
      Dim password As New SecureString()
      Dim top, left As Integer
      ' The Console.TreatControlCAsInput property prevents CTRL+C from
      ' ending this example.
      Console.TreatControlCAsInput = True
      Console.Clear()
      Console.WriteLine(m)
      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.
      Do
         cki = Console.ReadKey(True)
         If cki.Key = ConsoleKey.Escape Then Exit Do
         If cki.Key = ConsoleKey.Backspace Then
            If password.Length > 0 Then
               Console.SetCursorPosition(left + password.Length - 1, top)
               Console.Write(" "c)
               Console.SetCursorPosition(left + password.Length - 1, top)
               password.RemoveAt(password.Length - 1)
            End If
         Else
            If password.Length < 15 AndAlso([Char].IsLetterOrDigit(cki.KeyChar) _
            OrElse cki.KeyChar = "_"c) Then
               password.AppendChar(cki.KeyChar)
               Console.SetCursorPosition(left + password.Length - 1, top)
               Console.Write("*"c)
            End If
         End If
      Loop While cki.Key <> ConsoleKey.Enter And password.Length < 15
      ' Make the password read-only to prevent modification.
      password.MakeReadOnly()
      ' Dispose of the SecureString instance.
      password.Dispose()
   End Sub
End Class
' The example displays output like the following:
'    Enter your password (up to 15 letters, numbers, and underscores)
'    Press BACKSPACE to delete the last character entered.
'    Press Enter when done, or ESCAPE to quit:
'    ************
注解
使用构造函数初始化 类实例SecureString的文本值,并使用 、、RemoveAtSetAt、 InsertAt和 AppendChar 方法修改值Clear。SecureString
完成最终修改后,使用 MakeReadOnly 方法使实例的值不可变 (只读) 。 将值标记为只读后,进一步修改它的任何尝试都将 InvalidOperationException引发 。
调用 MakeReadOnly 的效果是永久性的, SecureString 因为 类不提供使安全字符串再次可修改的方法。 IsReadOnly使用 方法测试 的SecureString实例是否为只读。