SecureString Construtores

Definição

Inicializa uma nova instância da SecureString classe.

Sobrecargas

Name Description
SecureString()

Inicializa uma nova instância da SecureString classe.

SecureString(Char*, Int32)

Inicializa uma nova instância da SecureString classe a partir de um subarray de Char objetos.

Este construtor não é compatível com CLS. A alternativa compatível com CLS é SecureString().

SecureString()

Inicializa uma nova instância da SecureString classe.

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

Exceções

Ocorreu um erro ao proteger ou desproteger o valor desta instância.

Esta operação não é suportada nesta plataforma.

Exemplos

O exemplo seguinte usa o construtor padrão (ou sem parâmetros) para instanciar um novo SecureString objeto. Depois, chama o AppendChar método para adicionar um array de caracteres.

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.

O exemplo seguinte cria um SecureString objeto a partir do valor de um String objeto.

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.

Aplica-se a

SecureString(Char*, Int32)

Importante

Esta API não está em conformidade com CLS.

Inicializa uma nova instância da SecureString classe a partir de um subarray de Char objetos.

Este construtor não é compatível com CLS. A alternativa compatível com 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

Parâmetros

value
Char*

Um apontador para um array de Char objetos.

length
Int32

O número de elementos de value a incluir na nova instância.

Atributos

Exceções

value é null.

length é inferior a zero ou superior a 65.536.

Ocorreu um erro ao proteger ou desproteger o valor desta cadeia segura.

Esta operação não é suportada nesta plataforma.

Exemplos

O exemplo seguinte instancia um novo SecureString objeto ao passar ao seu construtor um ponteiro para um array de caracteres.

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.

Observações

Este construtor inicializa o novo SecureString objeto ao número de caracteres especificados value por length; o valor da instância é então encriptado.

Em C#, este construtor é definido apenas no contexto de código inseguro.

Aplica-se a