For TFS 2015 and above version, to connect to TFS using specific credentials, you could refer below code snippet:
To connect to TFS using specific credentials:
// For TFS 2015 & above
// Ultimately you want a VssCredentials instance so...
NetworkCredential netCred = new NetworkCredential(@"user.name", @"Password1", "DOMAIN");
WindowsCredential winCred = new WindowsCredential(netCred);
VssCredentials vssCred = new VssClientCredentials(winCred);
// Now you can connect to TFS passing Uri and VssCredentials instances as parameters
Uri tfsUri = new Uri(@"http://tfs:8080/tfs");
var tfsTeamProjectCollection = new TfsTeamProjectCollection(tfsUri, vssCred);
// Finally, to make sure you are authenticated...
tfsTeamProjectCollection.EnsureAuthenticated();
Besides, you could also choose to use PAT token, if you are using TFS2017 and above.
Especially basic authentication blocked on some repositories or environment .
Just create Personal Access Token (PAT) in the repository. Then use that in your connections to access TFS.
More details please take a look at C.J's answer in this case: Connect to TFS programmatically from vs 2017