3
votes

I have local tfs 2012. On Visual studio 2012 , use c# , I write program which programmatically connect to tfs server. But I have error:

{"TF30063: You are not authorized to access http://server:8080/tfs."} System.Exception {Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException}

My code:

Uri collectionUri = new Uri("http://server:8080/tfs");
NetworkCredential netCred = new NetworkCredential("login","password");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, netCred);
teamProjectCollection.EnsureAuthenticated();//throw error here

Can you help me fix this error?

P.S. I try do connect this way, but I have same error:

var projectCollection = new TfsTeamProjectCollection(
new Uri("http://myserver:8080/tfs/DefaultCollection"), 
new NetworkCredential("youruser", "yourpassword"));

projectCollection.Connect(ConnectOptions.IncludeServices);

And this way:

Uri collectionUri = new Uri("http://server:8080/tfs/DefaultCollection");
NetworkCredential netCred = new NetworkCredential("login","password","Server.local");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, netCred);
teamProjectCollection.EnsureAuthenticated();
1
Have you tried using the collection name in the uri... Uri collectionUri = new Uri("server:8080/tfs/<CollectionName>"); - Suresh Kumar Veluswamy
Also if you are in a domain, you should use the domain name for credentials.... NetworkCredential netCred = new NetworkCredential("login","password","DomainName"); - Suresh Kumar Veluswamy

1 Answers

5
votes

Hope it helps you.

var tfsCon = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://server:8080/tfs"));
tfsCon.Authenticate();
var workItems = new WorkItemStore(tfsCon);
var projectsList = (from Project p in workItems.Projects select p.Name).ToList();

or

Uri TfsURL = new Uri(""http://server:8080/tfs"");
NetworkCredential credential = new NetworkCredential(Username, Password, Domain);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(TfsURL, credential);
collection.EnsureAuthenticated();

If fails here you need to configure in App.config to set the default proxy:

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <system.net>
      <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
   </system.net>
</configuration>