SecureString Costruttori
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe SecureString.
Overload
| Nome | Descrizione |
|---|---|
| SecureString() |
Inizializza una nuova istanza della classe SecureString. |
| SecureString(Char*, Int32) |
Inizializza una nuova istanza della SecureString classe da una sottomaschera di Char oggetti . Questo costruttore non è conforme a CLS. L'alternativa conforme a CLS è SecureString(). |
SecureString()
Inizializza una nuova istanza della classe SecureString.
public:
SecureString();
public SecureString();
Public Sub New ()
Eccezioni
Errore durante la protezione o la rimozione della protezione del valore di questa istanza.
Questa operazione non è supportata in questa piattaforma.
Esempio
Nell'esempio seguente viene utilizzato il costruttore predefinito (o senza parametri) per creare un'istanza di un nuovo SecureString oggetto. Chiama quindi il AppendChar metodo per aggiungere una matrice di caratteri.
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.
Nell'esempio seguente viene creato un SecureString oggetto dal valore di un String oggetto .
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.
Si applica a
SecureString(Char*, Int32)
Importante
Questa API non è conforme a CLS.
Inizializza una nuova istanza della SecureString classe da una sottomaschera di Char oggetti .
Questo costruttore non è conforme a CLS. L'alternativa conforme a 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
Parametri
- length
- Int32
Numero di elementi da value includere nella nuova istanza.
- Attributi
Eccezioni
value è null.
length è minore di zero o maggiore di 65.536.
Si è verificato un errore durante la protezione o l'annullamento della protezione del valore di questa stringa protetta.
Questa operazione non è supportata in questa piattaforma.
Esempio
Nell'esempio seguente viene creata un'istanza di un nuovo SecureString oggetto passando il relativo costruttore a un puntatore a una matrice di caratteri.
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.
Commenti
Questo costruttore inizializza il nuovo SecureString oggetto al numero di caratteri in value specificato da length. Il valore dell'istanza viene quindi crittografato.
In C# questo costruttore viene definito solo nel contesto di codice non sicuro.