oleksa
oleksa

Reputation: 4037

TfsTeamProjectCollection does not ask for credential

Dears

Please help me with VS add-in Tfs connection question.

I've wrote VS 2017 plugin that uses TfsTeamProjectCollection class to connect to the tfs server. Tfs server is hosted in the https://dev.azure.com

However on certain computers it fails to connect to the Tfs server. For example, I've tried to reproduce this and cleared all caches like described here. After clearing/connecting several cycles I've got the following:

Here is the source code I'm using to connect:

public override TfsTeamProjectCollection CreateCollection(WorkspaceInfo wi)
{
    var s = (overrideConfig ?? Config);

    var u = wi.ServerUri;
    var vssCred = new VssClientCredentials(); // GetCredentials(s.Vsts);
    Logger.Debug("getting collection for url:{0}", u);
    TfsTeamProjectCollection tpc = null;
    try
    {
        tpc = new TfsTeamProjectCollection(u, vssCred);
        Logger.Debug("authenticating");

        tpc.Authenticate(); // 401 non authorized exception here 
    }

I've tried to add vssCred.Storage = new VssClientCredentialStorage(); before Autheticate() but no luck

Then I've checked network packets using Fiddler application. It shows the same request packet is sent using unit test, console application and add-in. But console application and unit test produces initial request

POST https://dev.azure.com/quipu-cwnet/Services/v3.0/LocationService.asmx HTTP/1.1

and response is

HTTP/1.1 401 Unauthorized

X-TFS-FedAuthRedirect: https://app.vssps.visualstudio.com/_signin?realm=dev.azure.com&reply_to=https%3A%2F%2Fdev.azure.com%2F...

Then console and unit test sends packet to the redirect url and asks for user credentials.

When add-in tries to connect to the Tfs server fiddler shows only first POST request and 401 response (with the same X-TFS-FedAuthRedirect header. But there is no redirection. From user side it looks like application freezes for some period after 401 response is obtained and then fails with 401 error.

Both Unit test and add-in assemblies has the same references to the 15.0.0 assemblies like Microsoft.TeamFoundation.VersionControl.Client and related.

I've tried VssConnection but the same

VssConnection connection = new VssConnection(u, vssCred);
connection.ConnectAsync().SyncResult();

var prj = connection.GetClient<ProjectHttpClient>();
var prjCollection = prj.GetProjects().Result; // 401 error here when is executed as VS add-in

foreach (var pc in prjCollection)
    Logger.Debug("\tVssConnection project {0} {1}", pc.Id, pc.Name);

I'm using the same user to start VS (and add-in) and console. No elevated permissions.

Is there anything I can do with TfsConnection when used as VS add-in ?

Upvotes: 0

Views: 975

Answers (1)

oleksa
oleksa

Reputation: 4037

the answer is to execute Authenticate() in the separate thread (non GUI) :crazy

var authTask = Task.Run(() => tpc.Authenticate());
authTask.Wait();

Please note that I'm using VS 2017 15.9.11 and Microsoft.TeamFoundationServer.ExtendedClient.15.112.1

upd1: I've upgraded Microsoft.TeamFoundationServer.ExtendedClient to 15.113.1 it works on my machine but still fails on my colleague machine.

Tfs credentials are working like a charm on both machines:

var tfsClientCredentials = TfsClientCredentials.LoadCachedCredentials(wi.ServerUri, false, false);

var res = new TfsTeamProjectCollection(wi.ServerUri, tfsClientCredentials);
res.EnsureAuthenticated();

despite the fact it is marked as obsolete.

Upvotes: 1

Related Questions