代码之家  ›  专栏  ›  技术社区  ›  KallDrexx

如何使用特定凭据连接到C中的TFS服务器?

  •  6
  • KallDrexx  · 技术社区  · 16 年前

    我正在尝试编写一个连接到TFS并检索工作项信息的C应用程序。不幸的是,似乎所有使用TFSDK的示例都使用当前用户的默认凭据(即我的域登录信息)。我找到的最接近的信息是使用 TeamFoundationServer (String, ICredentials) 但是,我找不到与 ICredentials 接口(尤其是因为它似乎不使用System.NET ICredentials,而是使用TeamFoundationServer特定的ICredentials)。

    是否有人对使用特定的用户名/密码/域组合登录到TFS有任何了解?

    3 回复  |  直到 7 年前
        1
  •  16
  •   Robaticus    16 年前

    以下代码将帮助您:

    NetworkCredential cred = new NetworkCredential("Username", "Password", "Domain");
    tfs = new TeamFoundationServer("http://tfs:8080/tfs", cred);
    tfs.EnsureAuthenticated();
    

    域是实际的域,或者在工作组情况下,它是承载TFS应用层的服务器的名称。

        2
  •  11
  •   ΩmegaMan    7 年前

    对于TFS 2015和2017,所提及的对象和方法已被(或正在)弃用。

    要使用特定凭据连接到TFS,请执行以下操作:

    // For TFS 2015 & 2017
    
    // 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);
    
    // Bonus - if you want to remain in control when
    // credentials are wrong, set 'CredentialPromptType.DoNotPrompt'.
    // This will thrown exception 'TFS30063' (without hanging!).
    // Then you can handle accordingly.
    vssCred.PromptType = CredentialPromptType.DoNotPrompt;
    
    // 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();
    
        3
  •  3
  •   Riegardt Steyn AdrianD    10 年前

    多年来,这就是您如何使用TFS 2013 API:

    // Connect to TFS Work Item Store
    ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
    Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, networkCredential);
    WorkItemStore witStore = new WorkItemStore(tfs);
    

    如果不起作用,请尝试通过其他人传递凭据 Credential 课程(为我工作):

    // Translate username and password to TFS Credentials
    ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
    WindowsCredential windowsCredential = new WindowsCredential(networkCredential);
    TfsClientCredentials tfsCredential = new TfsClientCredentials(windowsCredential, false);
    
    // Connect to TFS Work Item Store
    Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, tfsCredential);
    WorkItemStore witStore = new WorkItemStore(tfs);
    
    推荐文章