0

I am talking about the information visible if you open "Credential Manager" from the control panel or start menu. If you tell IE to save a password it's visible in there.

I already know about the wrapper for Advapi32.dll (http://www.nuget.org/packages/CredentialManagement/) that offers easy functionality of the functions:
CredReadW
CredWriteW
CredDeleteW
etc.

but at this point I am not even sure if those are the right functions to interact with website authentication data. I wasn't able to read existing or write new web authentication data with those functions (I don't even fully understand the credential types). Writing & reading CRED_TYPE_GENERIC credentials worked though.

How do I read and write website authentication data from C#?
I am ready to p/invoke if necessary.

ASA
  • 1,911
  • 3
  • 20
  • 37

1 Answers1

2

As you are using Windows 8, the simplest way is to use the WinRT API. I think you are creating a desktop application (Console, WPF, WinForms), so you have to:

  1. Add <TargetPlatformVersion>8.0</TargetPlatformVersion> to the csproj file.

    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{BEA97844-0370-40C1-A435-F95DC0DC0677}</ProjectGuid> <OutputType>Exe</OutputType> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <TargetPlatformVersion>8.0</TargetPlatformVersion>

  2. Add a reference to Windows and System.Runtime (look at the link below)

  3. Use the PasswordVault class

var vault = new Windows.Security.Credentials.PasswordVault(); var credentials = vault.RetrieveAll(); foreach (PasswordCredential credential in credentials) { Console.WriteLine("{0} {1}", credential.Resource, credential.UserName); }

Source: http://blogs.softfluent.com/post/2014/03/27/Acceder-a-API-WinRT-depuis-une-application-Desktop.aspx (French)

To get Windows and Generic credential: Encrypting credentials in a WPF application

Community
  • 1
  • 1
meziantou
  • 20,589
  • 7
  • 64
  • 83
  • This worked. Thanks for the explanation on modifying the .csproj file and adding system.runtime.dll, I already had that solution once but couldn't execute it. This gives me back all web login credentials but on the other side it doesn't give me back any windows login information (which I can access with the native functions mentioned above anyways). Why does Microsoft want us to jump through so many hoops? – ASA Mar 27 '14 at 09:45