以编程方式授权tfs的错误

时间:2014-03-25 08:29:13

标签: c# tfs

我有本地tfs 2012.在Visual Studio 2012上,使用c#,我编写以编程方式连接到tfs服务器的程序。但我有错误:

  

{" TF30063:您无权访问http://server:8080/tfs。"} System.Exception {Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException}

我的代码:

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

你能帮我解决这个错误吗?

P.S。我尝试以这种方式连接,但我有同样的错误:

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

projectCollection.Connect(ConnectOptions.IncludeServices);

就这样:

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 个答案:

答案 0 :(得分:4)

希望它对你有所帮助。

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();

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

如果在此失败,则需要在App.config中配置以设置默认代理:

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