Search code examples
c#authenticationtfsconnectiontfsbuild

Downloading Files from TFS using C#


I am trying to download my code files from TFS using C# programming. In order to achieve this there are two steps that needs to be followed,

  1. Make a connection to TFS through C#
  2. Route to the folder to download the required files.

I am currently working on step 1 and to achieve, i did some research and used TFS APIs and used below code,

 public static bool AlwaysGoodCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
    {
        return true;
    }
static void Main(string[] args)
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AlwaysGoodCertificate);
        // Translate username and password to TFS Credentials
        ICredentials networkCredential = new NetworkCredential(@"username", @"password", @"domain");
        WindowsCredential windowsCredential = new WindowsCredential(networkCredential);
        TfsClientCredentials tfsCredential = new TfsClientCredentials(windowsCredential, false);

        // Connect to TFS Work Item Store
        Uri tfsUri = new Uri(@"https://url");
        TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, tfsCredential);
        WorkItemStore witStore = new WorkItemStore(tfs);
}

Now, I am unable to connect to TFS through this code and whenever i try to connect using a browser by entering same url, password, domain and username I get logged in successfully but for some odd reason I am unable to log in using my code. I receive below error when i try to log in through code,

enter image description here


Solution

  • As Shayki Abramczyk suggested to use the collection name as well in the url.

    So I used the below URL and it solved my problem,

    https://server-name:443/tfs/Collection-name