在凭据管理器中访问Windows凭据

Pao*_*sco 5 c# security credential-manager

使用Windows.Security.Credentials.PasswordVault该类,我可以访问Windows凭据管理器中“ Web凭据”下存储的密码:

using System;
using Windows.Security.Credentials;

class Program {
    static void Main(string[] args) {
        PasswordVault vault = new PasswordVault();
        foreach (var cred in vault.RetrieveAll()) {
            cred.RetrievePassword();
            Console.WriteLine("Resource: {0}", cred.Resource);
            Console.WriteLine("UserName: {0}", cred.UserName);
            Console.WriteLine("Password: {0}", cred.Password);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一种方法可以代替检索存储在“ Windows凭据”下的凭据。

小智 4

这里的答案中有一个名为 CredentialManagement http://nuget.org/packages/CredentialManagement/的 Nuget 库:使用 C# 从 Windows 凭据存储中检索凭据

完美运作

        var cm = new Credential();
        cm.Target = "mycredentialname";

        if (!cm.Exists())
        {
            Console.WriteLine("cm is null");
        }
        cm.Load();
        Console.WriteLine("Password: " + cm.Password);
        Console.WriteLine("Username: " + cm.Username);
Run Code Online (Sandbox Code Playgroud)