如果您想直接插入用户凭据而不使用正在运行的进程,则以下内容对我有用:
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
}