SecureString Constructores

Definición

Inicializa una nueva instancia de la clase SecureString.Initializes a new instance of the SecureString class.

Sobrecargas

SecureString()

Inicializa una nueva instancia de la clase SecureString.Initializes a new instance of the SecureString class.

SecureString(Char*, Int32)

Inicializa una nueva instancia de la clase SecureString desde una submatriz de objetos Char.Initializes a new instance of the SecureString class from a subarray of Char objects.

Este constructor no es conforme a CLS.This constructor is not CLS-compliant. La alternativa conforme a CLS es SecureString().The CLS-compliant alternative is SecureString().

SecureString()

Inicializa una nueva instancia de la clase SecureString.Initializes a new instance of the SecureString class.

public:
 SecureString();
public SecureString ();
Public Sub New ()

Excepciones

Error al proteger o desproteger el valor de esta instancia.An error occurred while protecting or unprotecting the value of this instance.

Operación no permitida en esta plataforma.This operation is not supported on this platform.

Ejemplos

En el ejemplo siguiente se usa el constructor predeterminado (o sin parámetros) para crear una SecureString instancia de un nuevo objeto.The following example uses the default (or parameterless) constructor to instantiate a new SecureString object. A continuación, llama AppendChar al método para agregarle una matriz de caracteres.It then calls the AppendChar method to add an array of characters to it.

using namespace System;
using namespace System::Security;

int main(array<System::String ^> ^args)
{
   // Define the string value to assign to a new secure string.
   Char chars[4] = { 't', 'e', 's', 't' };
   // Instantiate the secure string.
   SecureString^ testString = gcnew SecureString();
   // Assign the character array to the secure string.
   for each (Char ch in chars)
   {
      testString->AppendChar(ch);
   }   
   // Display secure string length.
   Console::WriteLine("The length of the string is {0} characters.", 
                        testString->Length);

   delete testString;
   return 0;
}
// The example displays the following output:
//      The length of the string is 4 characters.
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.

En el ejemplo siguiente se SecureString crea un objeto a partir del String valor de un objeto.The following example creates a SecureString object from the value of a String object.

using namespace System;
using namespace System::Security;

int main(array<System::String ^> ^args)
{
   // Define the string value to be assigned to the secure string.
   String^ initString = "TestString";
   // Instantiate the secure string.
   SecureString^ testString = gcnew SecureString();
   // Assign the character array to the secure string.
   for each (Char ch in initString)
   {
      testString->AppendChar(ch);
   }   
   // Display secure string length.
   Console::WriteLine("The length of the string is {0} characters.", 
                        testString->Length);

   delete testString;
   return 0;
}
// The example displays the following output:
//      The length of the string is 10 characters.
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)

Importante

Esta API no es conforme a CLS.

Inicializa una nueva instancia de la clase SecureString desde una submatriz de objetos Char.Initializes a new instance of the SecureString class from a subarray of Char objects.

Este constructor no es conforme a CLS.This constructor is not CLS-compliant. La alternativa conforme a CLS es SecureString().The CLS-compliant alternative is SecureString().

public:
 SecureString(char* value, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public SecureString (char* value, int length);
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString

Parámetros

value
Char*

Puntero a una matriz de objetos Char.A pointer to an array of Char objects.

length
Int32

Número de elementos de value que se van a incluir en la nueva instancia.The number of elements of value to include in the new instance.

Atributos

Excepciones

value es null.value is null.

length es menor que cero o mayor que 65 536.length is less than zero or greater than 65,536.

Error al proteger o desproteger el valor de esta cadena segura.An error occurred while protecting or unprotecting the value of this secure string.

Operación no permitida en esta plataforma.This operation is not supported on this platform.

Ejemplos

En el ejemplo siguiente se crea una SecureString instancia de un nuevo objeto pasando a su constructor un puntero a una matriz de caracteres.The following example instantiates a new SecureString object by passing its constructor a pointer to a character array.

using namespace System;
using namespace System::Security;

int main(array<System::String ^> ^args)
{
   SecureString^ testString;
   // Define the string value to assign to a new secure string.
   Char chars[4] = { 't', 'e', 's', 't' };
   // Instantiate a new secure string.
   Char* pChars = &chars[0];

   testString = gcnew SecureString(pChars, sizeof(chars)/sizeof(chars[0]));

   // Display secure string length.
   Console::WriteLine("The length of the string is {0} characters.", 
                        testString->Length);
   delete testString;
   return 0;
}
// The example displays the following output:
//      The length of the string is 4 characters.
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.

Comentarios

Este constructor inicializa el nuevo SecureString objeto con el número de value caracteres especificado por length; el valor de la instancia se cifra después.This constructor initializes the new SecureString object to the number of characters in value specified by length; the value of the instance is then encrypted.

En C#, este constructor solo se define en el contexto del código no seguro.In C#, this constructor is defined only in the context of unsafe code.

Seguridad

SecurityCriticalAttribute
requiere plena confianza para el llamador inmediato.requires full trust for the immediate caller. Este miembro no puede ser utilizado por código transparente o de confianza parcial.This member cannot be used by partially trusted or transparent code.

Se aplica a