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 備註。
Secure |
初始化 SecureString 類別的新執行個體。 |
Secure |
從 Char 物件的子陣列,初始化 SecureString 類別的新執行個體。 這個建構函式不符合 CLS 標準。 符合 CLS 標準的替代項目為 SecureString()。 |
Length |
取得目前安全字串中的字元數。 |
Append |
將字元附加至目前安全字串的結尾。 |
Clear() |
刪除目前安全字串的值。 |
Copy() |
建立目前安全字串的複本。 |
Dispose() |
釋放由 SecureString 物件使用的所有資源。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Insert |
將這個安全字串中的字元插入指定索引位置。 |
Is |
指示這個安全字串是否標示為唯讀。 |
Make |
使這個安全字串的文字值成為唯讀。 |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Remove |
從這個安全字串移除位在指定索引位置的字元。 |
Set |
使用另一個字元,取代位在指定索引位置的現有字元。 |
To |
傳回代表目前物件的字串。 (繼承來源 Object) |
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |