Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 want to use the new basic authentication but I couldn't find a way to do it.

I found a lot of links to the blog post Team Foundation Service updates - Aug 27 but it is using the Team Explorer Everywhere Java client for TFS.

Is there a new version of the TFS .NET Object Model to support the basic authentication?

By the way I've successively logged in with the service account. This answer was very useful.

like image 728
Yordan Avatar asked Nov 06 '12 06:11

Yordan


People also ask

How do I add Visual Studio team services to Microsoft Teams?

From the list of connectors, we select the “Visual Studio Team Services” item shown in the list below by clicking the “Add” button. Microsoft Teams will ask you to select the Team Services account you would like to use, team project and event type to receive notifications for.

How to add and reauthenticate Visual Studio accounts using the enhanced workflow?

To use this enhanced workflow, you'll need to opt into using your system's default web browser as the mechanism to add and reauthenticate Visual Studio accounts. Not using this workflow could trigger a degraded experience resulting in multiple additional authentication prompts when adding or reauthenticating Visual Studio accounts.

Why does Visual Studio keep asking me to reauthenticate?

Versions of Visual Studio prior to 16.6 may have degraded authentication experiences when used with accounts that have enabled CA policies such as MFA, and are associated with two or more tenants. These issues can cause your instance of Visual Studio to prompt reauthentication multiple times per day.

What is Windows Authentication and how does it work with MVC?

When you enable Windows authentication, your web server becomes responsible for authenticating users. Typically, there are two different types of web servers that you use when creating and deploying an ASP.NET MVC application. First, while developing an MVC application, you use the ASP.NET Development Web Server included with Visual Studio.


1 Answers

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(
                "[email protected]",
                "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);
        }
    }
}
like image 163
Grant Holliday Avatar answered Oct 04 '22 02:10

Grant Holliday