SecureString クラス

定義

不要になったときにコンピューターのメモリから削除するなどして機密を保持する必要があるテキストを表します。 このクラスは継承できません。

public ref class SecureString sealed : IDisposable
public sealed class SecureString : IDisposable
type SecureString = class
    interface IDisposable
Public NotInheritable Class SecureString
Implements IDisposable
継承
SecureString
実装

次の例では、 を使用 SecureString してユーザーのパスワードをセキュリティで保護し、新しいプロセスを開始するための資格情報として使用する方法を示します。

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;

public class Example
{
    public static void Main()
    {
        // Instantiate the secure string.
        SecureString securePwd = new SecureString();
        ConsoleKeyInfo key;

        Console.Write("Enter password: ");
        do {
           key = Console.ReadKey(true);
           
           // Ignore any key out of range.
           if (((int) key.Key) >= 65 && ((int) key.Key <= 90)) {
              // Append the character to the password.
              securePwd.AppendChar(key.KeyChar);
              Console.Write("*");
           }   
        // Exit if Enter key is pressed.
        } while (key.Key != ConsoleKey.Enter);
        Console.WriteLine();
        
        try {
            Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN");
        }
        catch (Win32Exception e) {
            Console.WriteLine(e.Message);
        }
        finally {
           securePwd.Dispose();
        }
    }
}
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security

Public Class Example
    Public Shared Sub Main()
        ' Instantiate the secure string.
        Dim securePwd As New SecureString()
        Dim key As ConsoleKeyInfo
        
        Console.Write("Enter password: ")
        Do
           key = Console.ReadKey(True)

           ' Ignore any key out of range
           If CInt(key.Key) >= 65 And CInt(key.Key <= 90) Then    
              ' Append the character to the password.
              securePwd.AppendChar(key.KeyChar)
              Console.Write("*")
           End If                                    
        ' Exit if Enter key is pressed.
        Loop While key.Key <> ConsoleKey.Enter
        Console.WriteLine()
        
        Try
            Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN")
        Catch e As Win32Exception
            Console.WriteLine(e.Message)
        Finally
           securePwd.Dispose()
        End Try
    End Sub
End Class

注釈

この API の詳細については、「 SecureString の補足 API 解説」を参照してください。

コンストラクター

SecureString()

SecureString クラスの新しいインスタンスを初期化します。

SecureString(Char*, Int32)

Char オブジェクトのサブ配列から SecureString クラスの新しいインスタンスを初期化します。

このコンストラクターは、CLS 準拠ではありません。 CLS 準拠の代わりとして SecureString() を使用できます。

プロパティ

Length

現在のセキュリティ文字列内の文字数を取得します。

メソッド

AppendChar(Char)

現在のセキュリティ文字列の末尾に、文字を 1 つ追加します。

Clear()

現在のセキュリティ文字列の値を削除します。

Copy()

現在のセキュリティ文字列のコピーを作成します。

Dispose()

現在の SecureString オブジェクトによって使用されているすべてのリソースを解放します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InsertAt(Int32, Char)

このセキュリティ文字列の指定したインデックス位置に文字を挿入します。

IsReadOnly()

このセキュリティ文字列が読み取り専用としてマークされているかどうかを示します。

MakeReadOnly()

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

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
RemoveAt(Int32)

このセキュリティ文字列の指定されたインデックス位置にある文字を削除します。

SetAt(Int32, Char)

指定されたインデックス位置にある既存の文字を別の文字に置き換えます。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください