如何使用c#凭据管理器API在1个用户名下存储多个凭据?

时间:2017-03-20 13:11:38

标签: c# .net nuget-package credential-manager

我正在尝试为每个目标存储多个用户名和密码。问题是,每次使用相同的用户名但不同的目标和密码添加新凭据时,将删除具有相同用户名的其他凭据。

我的问题是如何在同一目标下存储多个用户名和密码?

public class PasswordRepository
{
    public string username;
    public string temp;
    LoggedIn li = new LoggedIn();
    public void SavePassword(string password, string PasswordName)
    {
        using (var cred = new Credential())
        {
            cred.Password = password;
            cred.Target = username;
            cred.Username = PasswordName;
            cred.Type = CredentialType.Generic;
            cred.PersistanceType = PersistanceType.LocalComputer;
            cred.Save();
        }
        temp = "";
    }

    public string GetPassword()
    {
        using (var cred = new Credential())
        {
            cred.Target = username;
            cred.Load();
            Debug.WriteLine(cred.Password +"    "+cred.Username);
            temp = cred.Password;
            return cred.Password;
        }
    }
}

https://www.nuget.org/packages/CredentialManagement/

1 个答案:

答案 0 :(得分:0)

您可以使用CredentialCache

来实现此目的
CredentialCache cache = new CredentialCache();
cache.Add(New Uri(myProxy.Url), "NTLM", New NetworkCredential("UserName", "Password", "Domain"))

MSDN