CredentialManager Class
- Supported on windows5.1.2600
Provides methods to manage credentials in the Windows Credential Manager.
[SupportedOSPlatform("windows5.1.2600")]
public static class CredentialManagerExample
// 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
| Name | Description |
|---|---|
| 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.csDeleteCredential(string, CredentialType)
CredentialManager.csDeletes a credential from the Windows Credential Manager.
public static void DeleteCredential(string applicationName, CredentialType type)Parameters
applicationNamestring- The name that identifies the credential.
typeCredentialType- The type of the credential.
Exceptions
ArgumentNullException- Thrown when
applicationNameisnull. Win32Exception- Thrown when no such credential exists, or when the credential cannot be deleted. Use
CredentialManager.TryDeleteCredentialto ignore a missing credential.
EnumerateCredentials overloads
EnumerateCredentials()
CredentialManager.csEnumerates 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.csEnumerates credentials from the Windows Credential Manager that match the specified filter.
public static IReadOnlyList<Credential> EnumerateCredentials(string? filter)Parameters
filterstring?- A filter string that supports wildcards. Pass
nullor "*" 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
ownernint- The handle of the parent window.
messageTextstring?- The message text to display in the dialog.
captionTextstring?- The caption text to display in the dialog.
userNamestring?- An optional default username.
saveCredentialCredentialSaveOption- 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
ownernint- The handle of the parent window.
messageTextstring?- The message text to display in the dialog.
captionTextstring?- The caption text to display in the dialog.
userNamestring?- An optional default username.
passwordstring?- An optional default password.
saveCredentialCredentialSaveOption- 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
ownernint- The handle of the parent window.
messageTextstring?- The message text to display in the dialog.
captionTextstring?- The caption text to display in the dialog.
userNamestring?- An optional default username.
passwordstring?- An optional default password.
saveCredentialCredentialSaveOption- Specifies whether the save checkbox should be displayed and its default state.
errorCredentialErrorCode- 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.csPrompts the user for credentials using a console-based interface.
public static CredentialResult? PromptForCredentialsConsole(string target, string? userName = null, CredentialSaveOption saveCredential = CredentialSaveOption.Unselected)Parameters
targetstring- The name of the target for the credentials.
userNamestring?- An optional default username.
saveCredentialCredentialSaveOption- 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.csReads a credential from the Windows Credential Manager.
public static Credential? ReadCredential(string applicationName)Parameters
applicationNamestring- The name that identifies the credential.
Returns
Credential? The credential if found; otherwise, null.
Exceptions
ArgumentNullException- Thrown when
applicationNameisnull.
ReadCredential(string, CredentialType)
CredentialManager.csReads a credential from the Windows Credential Manager.
public static Credential? ReadCredential(string applicationName, CredentialType type)Parameters
applicationNamestring- The name that identifies the credential.
typeCredentialType- The type of the credential.
Returns
Credential? The credential if found; otherwise, null.
Exceptions
ArgumentNullException- Thrown when
applicationNameisnull.
TryDeleteCredential overloads
TryDeleteCredential(string)
CredentialManager.csDeletes a credential from the Windows Credential Manager if it exists.
Parameters
applicationNamestring- The name that identifies the credential.
Returns
bool true if the credential was deleted; false if no such credential exists.
Exceptions
ArgumentNullException- Thrown when
applicationNameisnull. Win32Exception- Thrown when the credential exists but cannot be deleted.
TryDeleteCredential(string, CredentialType)
CredentialManager.csDeletes a credential from the Windows Credential Manager if it exists.
public static bool TryDeleteCredential(string applicationName, CredentialType type)Parameters
applicationNamestring- The name that identifies the credential.
typeCredentialType- The type of the credential.
Returns
bool true if the credential was deleted; false if no such credential exists.
Exceptions
ArgumentNullException- Thrown when
applicationNameisnull. Win32Exception- Thrown when the credential exists but cannot be deleted.
WriteCredential overloads
WriteCredential(string, string, string, CredentialPersistence)
CredentialManager.csWrites a credential to the Windows Credential Manager.
public static void WriteCredential(string applicationName, string userName, string secret, CredentialPersistence persistence)Parameters
applicationNamestring- The name that identifies the credential.
userNamestring- The username.
secretstring- The password or secret.
persistenceCredentialPersistence- The persistence option for the credential.
WriteCredential(string, string, string, CredentialPersistence, CredentialType)
CredentialManager.csWrites a credential to the Windows Credential Manager.
public static void WriteCredential(string applicationName, string userName, string secret, CredentialPersistence persistence, CredentialType type)Parameters
applicationNamestring- The name that identifies the credential.
userNamestring- The username.
secretstring- The password or secret.
persistenceCredentialPersistence- The persistence option for the credential.
typeCredentialType- The type of the credential.
WriteCredential(string, string, string, string?, CredentialPersistence)
CredentialManager.csWrites a credential to the Windows Credential Manager.
public static void WriteCredential(string applicationName, string userName, string secret, string? comment, CredentialPersistence persistence)Parameters
applicationNamestring- The name that identifies the credential.
userNamestring- The username.
secretstring- The password or secret.
commentstring?- An optional comment describing the credential.
persistenceCredentialPersistence- The persistence option for the credential.
WriteCredential(string, string, string, string?, CredentialPersistence, CredentialType)
CredentialManager.csWrites a credential to the Windows Credential Manager.
public static void WriteCredential(string applicationName, string userName, string secret, string? comment, CredentialPersistence persistence, CredentialType type)Parameters
applicationNamestring- The name that identifies the credential.
userNamestring- The username.
secretstring- The password or secret.
commentstring?- An optional comment describing the credential.
persistenceCredentialPersistence- The persistence option for the credential.
typeCredentialType- The type of the credential.
Exceptions
ArgumentNullException- Thrown when
applicationName,userName, orsecretisnull. ArgumentOutOfRangeException- Thrown when
secretexceeds 2560 bytes, whencommentexceeds 255 characters, or whentypeis notCredentialType.GenericorCredentialType.DomainPassword.