SecureString Konstruktoren

Definition

Initialisiert eine neue Instanz der SecureString-Klasse.

Überlädt

SecureString()

Initialisiert eine neue Instanz der SecureString-Klasse.

SecureString(Char*, Int32)

Initialisiert eine neue Instanz der SecureString-Klasse aus einem Unterarray von Char-Objekten.

Dieser Konstruktor ist nicht CLS-kompatibel. Die CLS-kompatible Alternative ist SecureString().

SecureString()

Quelle:
SecureString.cs
Quelle:
SecureString.cs
Quelle:
SecureString.cs

Initialisiert eine neue Instanz der SecureString-Klasse.

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

Ausnahmen

Beim Schützen oder Aufheben des Schutzes für den Wert dieser Instanz ist ein Fehler aufgetreten.

Diese Operation wird auf dieser Plattform nicht unterstützt.

Beispiele

Im folgenden Beispiel wird der Standardkonstruktor (oder der parameterlose Konstruktor) verwendet, um ein neues SecureString -Objekt zu instanziieren. Anschließend wird die AppendChar -Methode aufgerufen, um ihr ein Array von Zeichen hinzuzufügen.

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.

Im folgenden Beispiel wird ein SecureString -Objekt aus dem Wert eines String -Objekts erstellt.

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.

Gilt für:

SecureString(Char*, Int32)

Quelle:
SecureString.cs
Quelle:
SecureString.cs
Quelle:
SecureString.cs

Wichtig

Diese API ist nicht CLS-kompatibel.

Initialisiert eine neue Instanz der SecureString-Klasse aus einem Unterarray von Char-Objekten.

Dieser Konstruktor ist nicht CLS-kompatibel. Die CLS-kompatible Alternative ist 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

Parameter

value
Char*

Ein Zeiger auf ein Array von Char-Objekten.

length
Int32

Die Anzahl der Elemente von value, die in die neue Instanz eingeschlossen werden sollen.

Attribute

Ausnahmen

value ist null.

length ist kleiner als 0 (null) oder größer als 65.536.

Beim Schützen oder Aufheben des Schutzes für den Wert dieser sicheren Zeichenfolge ist ein Fehler aufgetreten.

Diese Operation wird auf dieser Plattform nicht unterstützt.

Beispiele

Im folgenden Beispiel wird ein neues SecureString Objekt instanziiert, indem seinem Konstruktor ein Zeiger auf ein Zeichenarray übergeben wird.

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.

Hinweise

Dieser Konstruktor initialisiert das neue SecureString Objekt mit der Anzahl von Zeichen in value . lengthDer Wert der Instanz wird dann verschlüsselt.

In C# wird dieser Konstruktor nur im Kontext von unsicherem Code definiert.

Gilt für: