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

How do you store multiple credentials under 1 username using the c# credential manager API?

我试图为每个目标存储 1 个以上的用户名和密码。问题是每次添加具有相同用户名但目标和密码不同的新凭据时,其他具有相同用户名的凭据都会被删除。

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

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/

您可以使用 CredentialCache 实现此目的

CredentialCache cache = new CredentialCache();
cache.Add(New Uri(myProxy.Url), "NTLM", New NetworkCredential("UserName", "Password", "Domain"))

MSDN