How do you connect to a TFS server in C# using specific credentials?

前端 未结 3 1226
轻奢々
轻奢々 2021-01-05 01:38

I am attempting to write a c# application that connects to TFS and retrieves work item information. Unfortunately, it seems like all examples of using the TFS SDK are using

相关标签:
3条回答
  • 2021-01-05 01:51

    Years down the line, this is how you do it with 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);
    

    If that doesn't work, try to pass the credentials through other Credential classes (worked for me):

    // 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);
    
    0 讨论(0)
  • 2021-01-05 01:53

    The following code should help you:

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

    Domain is either the actual domain, or in a Workgroup situation, it would be the name of the server that hosts the TFS Application Tier.

    0 讨论(0)
  • 2021-01-05 02:07

    For TFS 2015 & 2017, objects and methods mentioned have been (or are being) deprecated.

    To connect to TFS using specific credentials:

    // 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();
    
    0 讨论(0)
提交回复
热议问题