SecureString 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示应保密的文本,例如在不再需要时将其从计算机内存中删除。 此类不能被继承。
public ref class SecureString sealed : IDisposablepublic sealed class SecureString : IDisposabletype SecureString = class
    interface IDisposablePublic 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) |