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)

將字元附加至目前安全字串的結尾。

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)

適用於

另請參閱