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

前端 未结 2 1494
孤城傲影
孤城傲影 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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-12 03:44

    There have been some updates to authentication. For .NET apps we now recommend using VSTS client libraries. Another option is to use Azure Active Directory Libraries (ADAL). For more information and samples check out VSTS's authentication documentation.

    0 讨论(0)
提交回复
热议问题