SecureString 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SecureString 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| SecureString() |
初始化 SecureString 類別的新執行個體。 |
| SecureString(Char*, Int32) |
從一個物件子陣列Char初始化該SecureString類別的新實例。 此建構器不符合 CLS 標準。 符合 CLS 標準的替代方案為 SecureString()。 |
SecureString()
初始化 SecureString 類別的新執行個體。
public:
SecureString();
public SecureString();
Public Sub New ()
例外狀況
在保護或解除保護該實例值時發生錯誤。
此操作不支援此平台。
範例
以下範例使用預設(或無參數)建構子來實例化一個新 SecureString 物件。 接著它會呼叫該 AppendChar 方法,加入一個字元陣列。
using System;
using System.Security;
public class Example
{
public static void Main()
{
// Define the string value to assign to a new secure string.
char[] chars = { 't', 'e', 's', 't' };
// Instantiate the secure string.
SecureString testString = new SecureString();
// Assign the character array to the secure string.
foreach (char ch in chars)
testString.AppendChar(ch);
// Display secure string length.
Console.WriteLine("The length of the string is {0} characters.",
testString.Length);
testString.Dispose();
}
}
// The example displays the following output:
// The length of the string is 4 characters.
Imports System.Security
Module Example
Public Sub Main()
' Define the string value to assign to a new secure string.
Dim chars() As Char = { "t"c, "e"c, "s"c, "t"c }
' Instantiate the secure string.
Dim testString As SecureString = New SecureString()
' Assign the character array to the secure string.
For Each ch As char In chars
testString.AppendChar(ch)
Next
' Display secure string length.
Console.WriteLine("The length of the string is {0} characters.", _
testString.Length)
testString.Dispose()
End Sub
End Module
' The example displays the following output:
' The length of the string is 4 characters.
以下範例由物件String的值建立一個SecureString物件。
using System;
using System.Security;
public class Example
{
public static void Main()
{
// Define the string value to be assigned to the secure string.
string initString = "TestString";
// Instantiate the secure string.
SecureString testString = new SecureString();
// Use the AppendChar method to add each char value to the secure string.
foreach (char ch in initString)
testString.AppendChar(ch);
// Display secure string length.
Console.WriteLine("The length of the string is {0} characters.",
testString.Length);
testString.Dispose();
}
}
// The example displays the following output:
// The length of the string is 10 characters.
Imports System.Security
Module Example
Public Sub Main()
' Define the string value to be assigned to the secure string.
Dim initString As String = "TestString"
' Instantiate the secure string.
Dim testString As SecureString = New SecureString()
' Use the AppendChar method to add each char value to the secure string.
For Each ch As Char In initString
testString.AppendChar(ch)
Next
' Display secure string length.
Console.WriteLine("The length of the string is {0} characters.", _
testString.Length)
testString.Dispose()
End Sub
End Module
' The example displays the following output:
' The length of the string is 10 characters.
適用於
SecureString(Char*, Int32)
重要
此 API 不符合 CLS 規範。
從一個物件子陣列Char初始化該SecureString類別的新實例。
此建構器不符合 CLS 標準。 符合 CLS 標準的替代方案為 SecureString()。
public:
SecureString(char* value, int length);
[System.CLSCompliant(false)]
public SecureString(char* value, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public SecureString(char* value, int length);
[<System.CLSCompliant(false)>]
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString
參數
- length
- Int32
新實例中要包含的 value 元素數量。
- 屬性
例外狀況
value 是 null。
length 小於零或大於65,536。
在保護或解除保護這個安全字串值時發生錯誤。
此操作不支援此平台。
範例
以下範例透過將建構子傳遞指向字元陣列的指標來實例化一個新 SecureString 物件。
using System;
using System.Security;
public class Example
{
unsafe public static void Main()
{
SecureString testString;
// Define the string value to assign to a new secure string.
char[] chars = { 't', 'e', 's', 't' };
// Instantiate a new secure string.
fixed(char* pChars = chars)
{
testString = new SecureString(pChars, chars.Length);
}
// Display secure string length.
Console.WriteLine("The length of the string is {0} characters.",
testString.Length);
testString.Dispose();
}
}
// The example displays the following output:
// The length of the string is 4 characters.
備註
此建構器會將新 SecureString 物件初始化為 中 value 指定的 length字元數;接著該實例的值會被加密。
在 C# 中,這個建構子僅在不安全程式碼的上下文中被定義。