How to authenticate to Visual Studio Team Services with the new basic authentication from a .Net Windows Service?

前端 未结 2 1498
孤城傲影
孤城傲影 2021-01-12 02:59

I am trying to access https://visualstudio.com (formerly known as https://tfs.visualstudio.com, http://www.tfspreview.com) from my Windows Service written on .NET.

I

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 03:38

    First of all, you need to have at least Visual Studio 2012 Update 1 installed on your machine. It includes an updated Microsoft.TeamFoundation.Client.dll assembly with the BasicAuthCredential class.

    Here's the code to do it, from Buck's blog post How to connect to Team Foundation Service.

    using System;
    using System.Net;
    using Microsoft.TeamFoundation.Client;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                NetworkCredential netCred = new NetworkCredential(
                    "yourbasicauthusername@live.com",
                    "yourbasicauthpassword");
                BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
                TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
                tfsCred.AllowInteractive = false;
    
                TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
                    new Uri("https://YourAccountName.visualstudio.com/DefaultCollection"),
                    tfsCred);
    
                tpc.Authenticate();
    
                Console.WriteLine(tpc.InstanceId);
            }
        }
    }
    

提交回复
热议问题