SecureString Class

Definition

Represents text that should be kept confidential, such as by deleting it from computer memory when no longer needed. This class cannot be inherited.

public sealed class SecureString : IDisposable
Inheritance
SecureString
Implements

Examples

The following example demonstrates how to use a SecureString to secure a user's password for use as a credential to start a new process.

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();
        }
    }
}

Remarks

For more information about this API, see Supplemental API remarks for SecureString.

Constructors

SecureString()

Initializes a new instance of the SecureString class.

SecureString(Char*, Int32)

Initializes a new instance of the SecureString class from a subarray of Char objects.

This constructor is not CLS-compliant. The CLS-compliant alternative is SecureString().

Properties

Length

Gets the number of characters in the current secure string.

Methods

AppendChar(Char)

Appends a character to the end of the current secure string.

Clear()

Deletes the value of the current secure string.

Copy()

Creates a copy of the current secure string.

Dispose()

Releases all resources used by the current SecureString object.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InsertAt(Int32, Char)

Inserts a character in this secure string at the specified index position.

IsReadOnly()

Indicates whether this secure string is marked read-only.

MakeReadOnly()

Makes the text value of this secure string read-only.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RemoveAt(Int32)

Removes the character at the specified index position from this secure string.

SetAt(Int32, Char)

Replaces the existing character at the specified index position with another character.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also