0

当代码运行到第 2 行时,Windows 将显示一个弹出窗口以输入用户名、TFS 访问密码:

TeamProjectCollection = new TfsTeamProjectCollection(new Uri(url), new UICredentialsProvider());
TeamProjectCollection.Connect(Microsoft.TeamFoundation.Framework.Common.ConnectOptions.IncludeServices);
TeamProjectCollection.EnsureAuthenticated();
IsConnected = true;

有任何方法可以自动分配用户名、密码,而无需从弹出窗口登录。

谢谢。

4

1 回答 1

0

如果您想直接插入用户凭据而不使用正在运行的进程,则以下内容对我有用:

WindowsCredential credentials = new WindowsCredential(new NetworkCredential(username, domain, password), new MyCredentials(username, domain, password));
TfsTeamProjectCollection connectedTPC = new TfsTeamProjectCollection(tfsUrl, new TfsClientCredentials(credentials));

提供两次凭据有点奇怪,但是如果没有 MyCredentials,我将不会从 TFS 得到任何答复,不知道为什么。

public class MyCredentials : ICredentialsProvider
{
    private NetworkCredential credentials;
    #region ICredentialsProvider Members
    public MyCredentials(string user, string domain, string password)
    {
        credentials = new NetworkCredential(user, password, domain);
    }

    public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
    {
        return credentials;
    }

    public void NotifyCredentialsAuthenticated(Uri uri)
    {
        // who cares
    }

    #endregion
}
于 2013-03-08T14:55:26.487 回答