If you want to directly insert user credentials and not use the one the process is running, the following worked for me:
WindowsCredential credentials = new WindowsCredential(new NetworkCredential(username, domain, password), new MyCredentials(username, domain, password));
TfsTeamProjectCollection connectedTPC = new TfsTeamProjectCollection(tfsUrl, new TfsClientCredentials(credentials));
It's a bit strange to provide credentials two times, but without the MyCredentials I will get no answer from TFS, not sure why.
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
}