45

我只是想查询凭据存储(或在 Windows 8 中调用的Vault)并获取登录数据。MSDN 在这种情况下真的没有帮助,我也不想要任何 C++ P/Invoke方法。

我知道类似的问题已经在这里问过几次了,但是这些解决方案都不适用于我的情况。我不使用 Metro App 编程,所以PasswordVault(看起来)不可用。我只是创建了一个简单的 C# WPF 桌面应用程序。

理想情况下,它应该适用于多个 Windows 版本,但首选 Windows 8。

更具体地说,我想从 Outlook 的 CRM 插件查询存储的数据,以自动让我的应用程序登录到 CRM 服务器,而无需用户询问他/她的凭据。这意味着,如果这甚至可能......

那么如何访问 Windows Credentials Store?

4

4 回答 4

60

我一直在使用一个 NuGet 库,名为CredentialManagement

用法很简单。我把它包裹了一点,但我可能不需要:

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();
    }
}

示例用法:

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);

如果您有兴趣自己实现它,请浏览源代码: http ://credentialmanagement.codeplex.com/SourceControl/latest

诀窍是凭据管理器中没有 C# API。这个库很好地包装了其他 .dll 入口点。:-)

于 2013-07-19T13:13:58.163 回答
4

这适用于Windows Server 2012。我没有要测试的 Windows 8 机器。

在 .NET 桌面应用程序中使用 Windows 8 WinRT API

简而言之

  1. 卸载项目文件
  2. 编辑它
  3. 将 <TargetPlatformVersion>8.0</TargetPlatformVersion> 添加到PropertyGroup部分
  4. 添加对 Windows.Security 的引用(您将拥有一个 Windows 库列表)
  5. 添加System.Runtime.WindowsRuntime.dll位于C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5

然后你可以使用它(从这里):

private string resourceName = "My App";
private string defaultUserName;

private void Login()
{
    var loginCredential = GetCredentialFromLocker();

    if (loginCredential != null)
    {
        // There is a credential stored in the locker.
        // Populate the Password property of the credential
        // for automatic login.
        loginCredential.RetrievePassword();
    }
    else
    {
        // There is no credential stored in the locker.
        // Display UI to get user credentials.
        loginCredential = GetLoginCredentialUI();
    }

    // Log the user in.
    ServerLogin(loginCredential.UserName, loginCredential.Password);
}


private Windows.Security.Credentials.PasswordCredential GetCredentialFromLocker()
{
    Windows.Security.Credentials.PasswordCredential credential = null;

    var vault = new Windows.Security.Credentials.PasswordVault();
    var credentialList = vault.FindAllByResource(resourceName);
    if (credentialList.Count > 0)
    {
        if (credentialList.Count == 1)
        {
            credential = credentialList[0];
        }
        else
        {
            // When there are multiple usernames,
            // retrieve the default username. If one doesn’t
            // exist, then display UI to have the user select
            // a default username.

            defaultUserName = GetDefaultUserNameUI();

            credential = vault.Retrieve(resourceName, defaultUserName);
        }
    }
    return credential;
}
于 2014-06-09T08:26:59.083 回答
3

Randy 的答案System.String用于存储密码,这是不安全的。您将要用System.Security.SecureString于此目的。

如果您只是阅读.NET Framework 2.0 的凭据管理,您会做得更好。

于 2014-05-08T15:19:42.603 回答
0

使用 CredentialManagement(查看答案Retrieve credentials from Windows Credentials Store using C#)。

使用 PowerShell 也可能很有用:

CredMan.ps1 https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde

我无法列出存储的所有凭据。

using CredentialManagement;
using System.Diagnostics;

namespace UnitTestProject1
{
    [TestClass]
    public class CredentialTests
    {

        [TestMethod]
        public void Set_Credentials_for_older_domain_whe_migration_to_new_domain()
        {
            var accesos = new List<string> {
            "intranet",
            "intranet.xxxxx.net",
            "intranet.zzzzzzzz.com",
            "intranetescritorio.zzzzzzzz.net",
            "more...",
            };

            accesos.ForEach(acceso => SaveCredential(acceso));
        }

        private static Credential SaveCredential(string CredentialName)
        {
            var UserName = @"OLDERDOMAIN\user";
            var Password = "pass";

            var cm = new Credential { Target = CredentialName, Type = CredentialType.DomainPassword };
            if (cm.Exists())
            {
                cm.Load();
                Console.WriteLine("Credential " + cm.Target + ". Data: " + cm.Username + " " + cm.Password);

                //if (cm.Type == CredentialType.Generic)  cm.Delete();

                return cm;
            }

            cm = new Credential
            {
                Target = CredentialName,
                Type = CredentialType.DomainPassword,
                PersistanceType = PersistanceType.Enterprise,
                Username = UserName,
                Password = Password
            };
            cm.Save();
            return cm;
        }
    }
于 2015-01-02T12:27:10.077 回答