SecureString Classe

Definizione

Rappresenta il testo che deve essere mantenuto riservato, ad esempio eliminandolo dalla memoria del computer quando non è più necessario. La classe non può essere ereditata.

public ref class SecureString sealed : IDisposable
public sealed class SecureString : IDisposable
type SecureString = class
    interface IDisposable
Public NotInheritable Class SecureString
Implements IDisposable
Ereditarietà
SecureString
Implementazioni

Esempio

Nell'esempio seguente viene illustrato come usare un SecureString oggetto per proteggere la password di un utente da usare come credenziale per avviare un nuovo processo.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;

public class Example
{
    public static void Main()
    {
        // Instantiate the secure string.
        SecureString securePwd = new SecureString();
        ConsoleKeyInfo key;

        Console.Write("Enter password: ");
        do {
           key = Console.ReadKey(true);
           
           // Ignore any key out of range.
           if (((int) key.Key) >= 65 && ((int) key.Key <= 90)) {
              // Append the character to the password.
              securePwd.AppendChar(key.KeyChar);
              Console.Write("*");
           }   
        // Exit if Enter key is pressed.
        } while (key.Key != ConsoleKey.Enter);
        Console.WriteLine();
        
        try {
            Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN");
        }
        catch (Win32Exception e) {
            Console.WriteLine(e.Message);
        }
        finally {
           securePwd.Dispose();
        }
    }
}
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security

Public Class Example
    Public Shared Sub Main()
        ' Instantiate the secure string.
        Dim securePwd As New SecureString()
        Dim key As ConsoleKeyInfo
        
        Console.Write("Enter password: ")
        Do
           key = Console.ReadKey(True)

           ' Ignore any key out of range
           If CInt(key.Key) >= 65 And CInt(key.Key <= 90) Then    
              ' Append the character to the password.
              securePwd.AppendChar(key.KeyChar)
              Console.Write("*")
           End If                                    
        ' Exit if Enter key is pressed.
        Loop While key.Key <> ConsoleKey.Enter
        Console.WriteLine()
        
        Try
            Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN")
        Catch e As Win32Exception
            Console.WriteLine(e.Message)
        Finally
           securePwd.Dispose()
        End Try
    End Sub
End Class

Commenti

Per altre informazioni su questa API, vedere Osservazioni supplementari sull'API per SecureString.

Costruttori

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().

Proprietà

Nome Descrizione
Length

Ottiene il numero di caratteri nella stringa protetta corrente.

Metodi

Nome Descrizione
AppendChar(Char)

Aggiunge un carattere alla fine della stringa protetta corrente.

Clear()

Elimina il valore della stringa protetta corrente.

Copy()

Crea una copia della stringa protetta corrente.

Dispose()

Rilascia tutte le risorse utilizzate dall'oggetto corrente SecureString .

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
InsertAt(Int32, Char)

Inserisce un carattere in questa stringa sicura in corrispondenza della posizione di indice specificata.

IsReadOnly()

Indica se questa stringa sicura è contrassegnata come di sola lettura.

MakeReadOnly()

Rende il valore di testo di questa stringa sicura di sola lettura.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
RemoveAt(Int32)

Rimuove il carattere in corrispondenza della posizione di indice specificata da questa stringa protetta.

SetAt(Int32, Char)

Sostituisce il carattere esistente nella posizione di indice specificata con un altro carattere.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche