LsaPrivateData Class

  • Supported on windows5.1.2600

Provides methods to interact with Local Security Authority (LSA) private data storage on Windows. LSA private data storage keeps values encrypted on disk under a DACL that allows only the creator and administrators to read them.

[SupportedOSPlatform("windows5.1.2600")]
public static class LsaPrivateData

Remarks

Microsoft recommends CryptProtectData and CryptUnprotectData over the LSA private data functions unless you specifically need to manipulate LSA secrets. The stored data is not absolutely protected.

The key name decides how far the secret reaches. A name starting with L$ creates a local object that cannot be accessed remotely, G$ a global object, and M$ a machine object that only the operating system can read back. A name with none of those prefixes creates an object that can be accessed remotely, so prefer an L$ prefix unless you need otherwise.

Example

Store and retrieve a secret value:

// Store a secret value (requires administrator privileges)
LsaPrivateData.SetValue("L$MySecretKey", "MySecretValue");

// Retrieve the value
string? value = LsaPrivateData.GetValue("L$MySecretKey");
Assert.Equal("MySecretValue", value);

// Remove the value (requires administrator privileges)
LsaPrivateData.RemoveValue("L$MySecretKey");

Methods

NameDescription
GetValue(string)Retrieves a value from LSA private data storage.
RemoveValue(string)Removes a value from LSA private data storage. Requires administrator privileges.
SetValue(string, string?)Stores a value in LSA private data storage. Requires administrator privileges.

GetValue(string)

LsaPrivateData.cs

Retrieves a value from LSA private data storage.

public static string? GetValue(string key)

Parameters

key string
The key of the value to retrieve. Cannot be null or empty.

Returns

string? The value associated with the key, or null if the key does not exist.

RemoveValue(string)

LsaPrivateData.cs

Removes a value from LSA private data storage. Requires administrator privileges.

public static void RemoveValue(string key)

Parameters

key string
The key of the value to remove.

Remarks

Removing a key that does not exist does nothing.

SetValue(string, string?)

LsaPrivateData.cs

Stores a value in LSA private data storage. Requires administrator privileges.

public static void SetValue(string key, string? value)

Parameters

key string
The key under which to store the value. Cannot be null or empty.
value string?
The value to store. If null, the key is removed; removing a key that does not exist does nothing.