SecureString 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示應該保密的文字,例如,視需要從計算機記憶體中刪除它。 此類別無法獲得繼承。
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 備註。
建構函式
| 名稱 | Description |
|---|---|
| SecureString() |
初始化 SecureString 類別的新執行個體。 |
| SecureString(Char*, Int32) |
從一個物件子陣列Char初始化該SecureString類別的新實例。 此建構器不符合 CLS 標準。 符合 CLS 標準的替代方案為 SecureString()。 |
屬性
| 名稱 | Description |
|---|---|
| Length |
取得目前安全字串的字元數。 |
方法
| 名稱 | Description |
|---|---|
| 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) |