SecureString Constructors

Definitie

Initialiseert een nieuw exemplaar van de SecureString klasse.

Overloads

Name Description
SecureString()

Initialiseert een nieuw exemplaar van de SecureString klasse.

SecureString(Char*, Int32)

Initialiseert een nieuw exemplaar van de SecureString klasse op basis van een submaarray van Char objecten.

Deze constructor is niet cls-compatibel. Het cls-compatibele alternatief is SecureString().

SecureString()

Initialiseert een nieuw exemplaar van de SecureString klasse.

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

Uitzonderingen

Er is een fout opgetreden tijdens het beveiligen of opheffen van de beveiliging van de waarde van dit exemplaar.

Deze bewerking wordt niet ondersteund op dit platform.

Voorbeelden

In het volgende voorbeeld wordt de standaardconstructor (of parameterloos) gebruikt om een nieuw SecureString object te instantiëren. Vervolgens wordt de AppendChar methode aangeroepen om er een matrix met tekens aan toe te voegen.

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.

In het volgende voorbeeld wordt een SecureString object gemaakt op basis van de waarde van een String object.

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.

Van toepassing op

SecureString(Char*, Int32)

Belangrijk

Deze API is niet CLS-conform.

Initialiseert een nieuw exemplaar van de SecureString klasse op basis van een submaarray van Char objecten.

Deze constructor is niet cls-compatibel. Het cls-compatibele alternatief is 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

Parameters

value
Char*

Een aanwijzer naar een matrix met Char objecten.

length
Int32

Het aantal elementen dat value moet worden opgenomen in het nieuwe exemplaar.

Kenmerken

Uitzonderingen

value is null.

length is kleiner dan nul of groter dan 65.536.

Er is een fout opgetreden tijdens het beveiligen of opheffen van de beveiliging van de waarde van deze beveiligde tekenreeks.

Deze bewerking wordt niet ondersteund op dit platform.

Voorbeelden

In het volgende voorbeeld wordt een nieuw SecureString object geïnstitueerd door de constructor een aanwijzer door te geven aan een tekenmatrix.

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.

Opmerkingen

Met deze constructor wordt het nieuwe SecureString object geïnitialiseerd tot het aantal tekens dat is value opgegeven door length; de waarde van het exemplaar wordt vervolgens versleuteld.

In C# wordt deze constructor alleen gedefinieerd in de context van onveilige code.

Van toepassing op