SecureString.MakeReadOnly メソッド

定義

このセキュリティ文字列のテキスト値を読み取り専用にします。

public:
 void MakeReadOnly();
public void MakeReadOnly ();
member this.MakeReadOnly : unit -> unit
Public Sub MakeReadOnly ()

例外

このセキュリティ文字列は既に破棄されています。

次の例は、 AppendChar およびメソッドを使用して RemoveAt パスワードの文字を収集する方法を示しています。 パスワードが収集されると、読み取り専用になります。

using namespace System;
using namespace System::Security;

void main()
{
   bool go = true;
   ConsoleKeyInfo cki;
   String^ m = L"\nEnter your password (up to 15 letters, numbers, and underscores)\n"
               L"Press BACKSPACE to delete the last character entered. " +
               L"\nPress Enter when done, or ESCAPE to quit:";
   SecureString ^ password = gcnew SecureString;
   int top;
   int 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;

   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.
   delete password;

}
// 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:
//    ************
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 Example
   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 SecureString 値を変更し Clear RemoveAt SetAt InsertAt AppendChar ます。

最終的な変更を行ったら、メソッドを使用して、 MakeReadOnly インスタンスの値を変更できないようにします (読み取り専用)。 値が読み取り専用としてマークされた後、それを変更しようとすると、がスローさ InvalidOperationException れます。

MakeReadOnly SecureString クラスはセキュリティで保護された文字列を再度変更可能にする手段を提供しないため、を呼び出すことによる効果は永続的です。 IsReadOnlyのインスタンスが読み取り専用かどうかをテストするには、メソッドを使用し SecureString ます。

適用対象

こちらもご覧ください