Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve credentials from Windows Credentials Store using C#

Tags:

c#

credentials

I simply want to query the Credentials Store (or Vault as it is called in Windows 8) and get the login data. MSDN is really unhelpful in this case, and I also do not want any C++ P/Invoke approaches.

I know that similar questions have been asked here a few times, but none of those solutions work in my case. I do not use Metro App programming, so things like PasswordVault are (as it looks) not available. I just create a simple C# WPF desktop application.

Ideally, it should work in several Windows versions, but Windows 8 is preferred.

More specifically, I want to query the stored data from the CRM plugin for Outlook to automatically have my application log in to the CRM Server without having the user to ask for his/her credentials. That means, if this is even possible...

So how do I access the Windows Credentials Store?

like image 397
Kirschi Avatar asked Jul 19 '13 08:07

Kirschi


People also ask

Is there a way to view password stored in Windows credentials?

If you need to see the list of your credentials, you may go to Control Panel > User Accounts > Credential Manager. You may click the dropdown arrow then click Show on Password field. Please note that it will ask you to re-enter the password to verify your identity.

What is the full path to the vault DLL file used to extract vault credentials?

C:\ProgramData\Microsoft\Vault.


1 Answers

There is a NuGet library that I've been using, called CredentialManagement.

The usage is pretty simple. I wrapped it a little, but I probably didn't need to:

public static class CredentialUtil {     public static UserPass GetCredential(string target)     {         var cm = new Credential {Target = target};         if (!cm.Load())         {             return null;         }          // UserPass is just a class with two string properties for user and pass         return new UserPass(cm.Username, cm.Password);     }      public static bool SetCredentials(          string target, string username, string password, PersistanceType persistenceType)     {        return new Credential {Target = target,                               Username = username,                               Password = password,                               PersistanceType =  persistenceType}.Save();     }      public static bool RemoveCredentials(string target)     {         return new Credential { Target = target }.Delete();     } } 

Sample usage:

CredentialUtil.SetCredentials("FOO", "john", "1234", PersistanceType.LocalComputer); var userpass = CredentialUtil.GetCredential("FOO"); Console.WriteLine($"User: {userpass.Username} Password: {userpass.Password}"); CredentialUtil.RemoveCredentials("FOO"); Debug.Assert(CredentialUtil.GetCredential("FOO") == null); 

If you're interested in implementing it yourself, browse the source: http://credentialmanagement.codeplex.com/SourceControl/latest

The trick is that there is no C# API into the Credential Manager. This library wraps the other .dll entry points nicely. :-)

like image 51
Randy James Avatar answered Oct 09 '22 00:10

Randy James