2010-05-13 111 views
9

我在MS Team Foundation Server上以特定用戶身份驗證時遇到問題。在舊版本中,它看起來像:在TFS 2010上進行身份驗證

teamFoundationCredential = new System.Net.NetworkCredential("<USERNAME>", "<PASSWORD>", "<DOMAIN>"); 

TeamFoundationServer tfs = new TeamFoundationServer("http://mars:8080/", teamFoundationCredential); 

有人可以告訴我2010版的equivilent。到目前爲止,我有:

ICredentialsProvider cred = null; 

tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://asebeast.cpsc.ucalgar.ca:8080/tfs/DefualtCollection")); 

tfs.EnsureAuthenticated(); 

感謝

回答

13

對於TFS 2010,使用以下命令:

TfsTeamProjectCollection collection = new TfsTeamProjectCollection(
     new Uri("http://asebeast.cpsc.ucalgar.ca:8080/tfs/DefaultCollection", 
     new System.Net.NetworkCredential("domain_name\\user_name", "pwd")); 
collection.EnsureAuthenticated(); 
+1

注意這個工作對我來說是無域服務器上,如果我刪除的用戶名的「domain_par」部分。 – Simon 2011-12-02 11:36:38

+1

我希望我能大約上百次。 – theo 2011-12-31 16:14:43

+1

另外:你不需要NetworkCredential(「用戶名」,「密碼」,「域」)而不是NetworkCredential(「用戶名\\域名」,「密碼」),它對我來說是第一種方式,但不是第二種。 – theo 2011-12-31 16:15:58

4

我一直有同樣的問題。上述解決方案不適合我,真的不知道爲什麼。我不斷收到演員異常。 花了一天的時間試圖弄清楚 - 所以想到我會分享我目前的解決方法,以解決問題。 我已經創建了實現ICredentialsProvider我自己的內部類 - 如下:

private 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) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

我然後實例化這個並把它傳遞中,如下所示:

MyCredentials credentials = new MyCredentials(UserName, Password, Domain); 
TfsTeamProjectCollection configurationServer = 
    TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
     new Uri(tfsUri), credentials); 

請注意,我還沒有實現的NotifyCredentialsAuthenticated - 不確定這實際上做了什麼,所以留下了NotImplementedException,這樣我就可以捕捉到它的調用時間,迄今爲止還沒有發生。現在已成功連接到TFS。

+1

您可能需要執行新的NetworkCredential(「username」,「password」,「domain」)而不是NetworkCredential(「username \\ domain」,「password」 – theo 2011-12-31 16:15:36

+0

這適用於我。 – AMissico 2012-10-10 01:17:08

0

我已經使用這種方法,以及連接到我們的老TFS 2008服務器的一些問題,但解決了我的情況下,事情真的很簡單:

首先,我定義的TFS網址爲:

private const string Tfs2008Url = @"http://servername:8080/tfs/"; 
static readonly Uri Tfs2008Uri = new Uri(Tfs2008Url); 

URL中使用的路徑是我們在通過VisualStudio連接時使用的路徑,所以我認爲這在API調用中必須是相同的,但是當我嘗試通過以下身份驗證使用該路徑時,我得到了一個TF31002/404錯誤:

var collection = new TfsTeamProjectCollection(Tfs2008Uri,new NetworkCredential("AdminUser","password","domain_name")); 
collection.EnsureAuthenticated(); 

但是當我將Url改爲TFS根目錄時,它通過了OK!

private const string Tfs2008Url = @"http://servername:8080/"; 
static readonly Uri Tfs2008Uri = new Uri(Tfs2008Url); 

不知道這是否幫助了任何人,但它確實爲我做了詭計!

0

這工作很適合我:

_tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri); 
_tfs.ClientCredentials = new TfsClientCredentials(new WindowsCredential(new NetworkCredential("myUserName", "qwerty_pwd", "myDomainName"))); 
_tfs.EnsureAuthenticated();