CredentialManager Class

  • Supported on windows5.1.2600

Provides methods to manage credentials in the Windows Credential Manager.

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

Example

// Save a credential
CredentialManager.WriteCredential(
    applicationName: "MyApp",
    userName: "user@example.com",
    secret: "password123",
    comment: "Application credentials",
    persistence: CredentialPersistence.LocalMachine);

// Read a credential
var credential = CredentialManager.ReadCredential("MyApp");
Assert.Equal("user@example.com", credential?.UserName);

// Delete a credential
CredentialManager.DeleteCredential("MyApp");

Methods

NameDescription
DeleteCredential(string)Deletes a credential from the Windows Credential Manager.
DeleteCredential(string, CredentialType)Deletes a credential from the Windows Credential Manager.
EnumerateCredentials()Enumerates all credentials from the Windows Credential Manager.
EnumerateCredentials(string?)Enumerates credentials from the Windows Credential Manager that match the specified filter.
PromptForCredentials(nint, string?, string?, string?, CredentialSaveOption)Prompts the user for credentials using a Windows dialog.
PromptForCredentials(nint, string?, string?, string?, string?, CredentialSaveOption)Prompts the user for credentials using a Windows dialog.
PromptForCredentials(nint, string?, string?, string?, string?, CredentialSaveOption, CredentialErrorCode)Prompts the user for credentials using a Windows dialog.
PromptForCredentialsConsole(string, string?, CredentialSaveOption)Prompts the user for credentials using a console-based interface.
ReadCredential(string)Reads a credential from the Windows Credential Manager.
ReadCredential(string, CredentialType)Reads a credential from the Windows Credential Manager.
TryDeleteCredential(string)Deletes a credential from the Windows Credential Manager if it exists.
TryDeleteCredential(string, CredentialType)Deletes a credential from the Windows Credential Manager if it exists.
WriteCredential(string, string, string, CredentialPersistence)Writes a credential to the Windows Credential Manager.
WriteCredential(string, string, string, CredentialPersistence, CredentialType)Writes a credential to the Windows Credential Manager.
WriteCredential(string, string, string, string?, CredentialPersistence)Writes a credential to the Windows Credential Manager.
WriteCredential(string, string, string, string?, CredentialPersistence, CredentialType)Writes a credential to the Windows Credential Manager.

DeleteCredential overloads

DeleteCredential(string)

CredentialManager.cs

Deletes a credential from the Windows Credential Manager.

public static void DeleteCredential(string applicationName)
Parameters
applicationName string
The name that identifies the credential.

DeleteCredential(string, CredentialType)

CredentialManager.cs

Deletes a credential from the Windows Credential Manager.

public static void DeleteCredential(string applicationName, CredentialType type)
Parameters
applicationName string
The name that identifies the credential.
type CredentialType
The type of the credential.
Exceptions
ArgumentNullException
Thrown when applicationName is null.
Win32Exception
Thrown when no such credential exists, or when the credential cannot be deleted. Use CredentialManager.TryDeleteCredential to ignore a missing credential.

EnumerateCredentials overloads

EnumerateCredentials()

CredentialManager.cs

Enumerates all credentials from the Windows Credential Manager.

public static IReadOnlyList<Credential> EnumerateCredentials()
Returns

IReadOnlyList<Credential> A read-only list of credentials, or an empty list when the credential set is empty.

EnumerateCredentials(string?)

CredentialManager.cs

Enumerates credentials from the Windows Credential Manager that match the specified filter.

public static IReadOnlyList<Credential> EnumerateCredentials(string? filter)
Parameters
filter string?
A filter string that supports wildcards. Pass null or "*" to enumerate all credentials.
Returns

IReadOnlyList<Credential> A read-only list of credentials matching the filter, or an empty list when no credential matches.

PromptForCredentials overloads

PromptForCredentials(nint, string?, string?, string?, CredentialSaveOption)

CredentialManager.cs
  • Supported on windows6.0.6000

Prompts the user for credentials using a Windows dialog.

[SupportedOSPlatform("windows6.0.6000")]
public static CredentialResult? PromptForCredentials(nint owner, string? messageText, string? captionText, string? userName, CredentialSaveOption saveCredential)
Parameters
owner nint
The handle of the parent window.
messageText string?
The message text to display in the dialog.
captionText string?
The caption text to display in the dialog.
userName string?
An optional default username.
saveCredential CredentialSaveOption
Specifies whether the save checkbox should be displayed and its default state.
Returns

CredentialResult? A CredentialResult if the user entered credentials; null if the user cancelled the prompt.

Exceptions
Win32Exception
Thrown when the prompt fails, or when the credentials it returned cannot be read.

PromptForCredentials(nint, string?, string?, string?, string?, CredentialSaveOption)

CredentialManager.cs
  • Supported on windows6.0.6000

Prompts the user for credentials using a Windows dialog.

[SupportedOSPlatform("windows6.0.6000")]
public static CredentialResult? PromptForCredentials(nint owner, string? messageText, string? captionText, string? userName, string? password, CredentialSaveOption saveCredential)
Parameters
owner nint
The handle of the parent window.
messageText string?
The message text to display in the dialog.
captionText string?
The caption text to display in the dialog.
userName string?
An optional default username.
password string?
An optional default password.
saveCredential CredentialSaveOption
Specifies whether the save checkbox should be displayed and its default state.
Returns

CredentialResult? A CredentialResult if the user entered credentials; null if the user cancelled the prompt.

Exceptions
Win32Exception
Thrown when the prompt fails, or when the credentials it returned cannot be read.

PromptForCredentials(nint, string?, string?, string?, string?, CredentialSaveOption, CredentialErrorCode)

CredentialManager.cs
  • Supported on windows6.0.6000

Prompts the user for credentials using a Windows dialog.

[SupportedOSPlatform("windows6.0.6000")]
public static CredentialResult? PromptForCredentials(nint owner = 0, string? messageText = null, string? captionText = null, string? userName = null, string? password = null, CredentialSaveOption saveCredential = CredentialSaveOption.Unselected, CredentialErrorCode error = CredentialErrorCode.None)
Parameters
owner nint
The handle of the parent window.
messageText string?
The message text to display in the dialog.
captionText string?
The caption text to display in the dialog.
userName string?
An optional default username.
password string?
An optional default password.
saveCredential CredentialSaveOption
Specifies whether the save checkbox should be displayed and its default state.
error CredentialErrorCode
Specifies an error code to display an error message.
Returns

CredentialResult? A CredentialResult if the user entered credentials; null if the user cancelled the prompt.

Exceptions
Win32Exception
Thrown when the prompt fails, or when the credentials it returned cannot be read.

PromptForCredentialsConsole(string, string?, CredentialSaveOption)

CredentialManager.cs

Prompts the user for credentials using a console-based interface.

public static CredentialResult? PromptForCredentialsConsole(string target, string? userName = null, CredentialSaveOption saveCredential = CredentialSaveOption.Unselected)

Parameters

target string
The name of the target for the credentials.
userName string?
An optional default username.
saveCredential CredentialSaveOption
Specifies whether the save checkbox should be displayed and its default state.

Returns

CredentialResult? A CredentialResult containing the entered credentials, or null if the user cancelled the prompt.

Exceptions

Win32Exception
Thrown when the prompt fails.

ReadCredential overloads

ReadCredential(string)

CredentialManager.cs

Reads a credential from the Windows Credential Manager.

public static Credential? ReadCredential(string applicationName)
Parameters
applicationName string
The name that identifies the credential.
Returns

Credential? The credential if found; otherwise, null.

Exceptions
ArgumentNullException
Thrown when applicationName is null.

ReadCredential(string, CredentialType)

CredentialManager.cs

Reads a credential from the Windows Credential Manager.

public static Credential? ReadCredential(string applicationName, CredentialType type)
Parameters
applicationName string
The name that identifies the credential.
type CredentialType
The type of the credential.
Returns

Credential? The credential if found; otherwise, null.

Exceptions
ArgumentNullException
Thrown when applicationName is null.

TryDeleteCredential overloads

TryDeleteCredential(string)

CredentialManager.cs

Deletes a credential from the Windows Credential Manager if it exists.

public static bool TryDeleteCredential(string applicationName)
Parameters
applicationName string
The name that identifies the credential.
Returns

bool true if the credential was deleted; false if no such credential exists.

Exceptions
ArgumentNullException
Thrown when applicationName is null.
Win32Exception
Thrown when the credential exists but cannot be deleted.

TryDeleteCredential(string, CredentialType)

CredentialManager.cs

Deletes a credential from the Windows Credential Manager if it exists.

public static bool TryDeleteCredential(string applicationName, CredentialType type)
Parameters
applicationName string
The name that identifies the credential.
type CredentialType
The type of the credential.
Returns

bool true if the credential was deleted; false if no such credential exists.

Exceptions
ArgumentNullException
Thrown when applicationName is null.
Win32Exception
Thrown when the credential exists but cannot be deleted.

WriteCredential overloads

WriteCredential(string, string, string, CredentialPersistence)

CredentialManager.cs

Writes a credential to the Windows Credential Manager.

public static void WriteCredential(string applicationName, string userName, string secret, CredentialPersistence persistence)
Parameters
applicationName string
The name that identifies the credential.
userName string
The username.
secret string
The password or secret.
persistence CredentialPersistence
The persistence option for the credential.

WriteCredential(string, string, string, CredentialPersistence, CredentialType)

CredentialManager.cs

Writes a credential to the Windows Credential Manager.

public static void WriteCredential(string applicationName, string userName, string secret, CredentialPersistence persistence, CredentialType type)
Parameters
applicationName string
The name that identifies the credential.
userName string
The username.
secret string
The password or secret.
persistence CredentialPersistence
The persistence option for the credential.
type CredentialType
The type of the credential.

WriteCredential(string, string, string, string?, CredentialPersistence)

CredentialManager.cs

Writes a credential to the Windows Credential Manager.

public static void WriteCredential(string applicationName, string userName, string secret, string? comment, CredentialPersistence persistence)
Parameters
applicationName string
The name that identifies the credential.
userName string
The username.
secret string
The password or secret.
comment string?
An optional comment describing the credential.
persistence CredentialPersistence
The persistence option for the credential.

WriteCredential(string, string, string, string?, CredentialPersistence, CredentialType)

CredentialManager.cs

Writes a credential to the Windows Credential Manager.

public static void WriteCredential(string applicationName, string userName, string secret, string? comment, CredentialPersistence persistence, CredentialType type)
Parameters
applicationName string
The name that identifies the credential.
userName string
The username.
secret string
The password or secret.
comment string?
An optional comment describing the credential.
persistence CredentialPersistence
The persistence option for the credential.
type CredentialType
The type of the credential.
Exceptions
ArgumentNullException
Thrown when applicationName, userName, or secret is null.
ArgumentOutOfRangeException
Thrown when secret exceeds 2560 bytes, when comment exceeds 255 characters, or when type is not CredentialType.Generic or CredentialType.DomainPassword.