- 1 year ago
Programmatically Create Windows Credential Manager Entries
To programmatically create Windows Credential Manager entries in C#, you can utilize the CredentialManagement
library, which provides a convenient way to interact with the Windows Credential Manager. Here's an example that demonstrates how to create a new credential entry:
Install the
CredentialManagement
library: Use NuGet Package Manager Console or Visual Studio's NuGet Package Manager to install theCredentialManagement
package in your C# project.Import the necessary namespaces: In your C# code file, import the following namespaces:
csharpusing CredentialManagement;
Create a new credential entry:
csharp// Create a new instance of the Credential object
var credential = new Credential
{
Target = "MyCredential",
Username = "myUsername",
Password = "myPassword",
PersistanceType = PersistanceType.Enterprise,
Type = CredentialType.Generic
};
// Save the credential entry to Windows Credential Manager
if (credential.Save())
{
Console.WriteLine("Credential entry created successfully.");
}
else
{
Console.WriteLine("Failed to create credential entry.");
}
In the example above, we create a new
Credential
object and set its properties, such asTarget
,Username
,Password
,PersistanceType
, andType
. Adjust these values according to your requirements.The
Save()
method saves the credential entry to the Windows Credential Manager. It returnstrue
if the operation is successful andfalse
otherwise.
By following these steps, you can programmatically create Windows Credential Manager entries using C#. Remember to handle any exceptions that may occur during the process and ensure that you have the necessary permissions to create credential entries on the target machine.