Szerkesztés

Megosztás a következőn keresztül:


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 ref class SecureString sealed : IDisposable
public sealed class SecureString : IDisposable
type SecureString = class
    interface IDisposable
Public NotInheritable Class SecureString
Implements 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();
        }
    }
}
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

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

See also