Team Services programmatic access is working for s

2020-02-16 05:22发布

问题:

We have recently upgraded to VS 2017 and Visual Studio Team Services.

We have a set of automated tools that perform various source control related tasks on behalf of developers.

Some users and one service account appear to have cached credentials somewhere and do not get prompted for credentials when running the tools.

However, other users get prompted for credentials.

I created a mini test app to illustrate the issue. NOTE: The VSTS dlls are the version 15.x files from the VS2017 Team Explorer installation folder. They are also available as part of the Microsoft.TeamFoundationServer.ExtendedClient NuGet package if you don't mind the bloat.

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace VSTSTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                if (args.Length > 0)
                {
                    var projectCollectionUrl = args[0];

                    var projectCollection = new TfsTeamProjectCollection(new Uri(projectCollectionUrl));
                    var versionControlServer = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));

                    Console.WriteLine("AuthorizedUser: {0}", versionControlServer.AuthorizedUser);
                }

                Console.WriteLine("Usage is {projectCollectionUrl}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }
    }
}

This works for some users but not for others and I am trying to find out why.

回答1:

Check this registry path: HKEY_CURRENT_USER\Software\Microsoft\VSCommon\14.0\ClientServices\TokenStorage\VisualStudio\VssApp (Tested in VS2017, it stores in 14.0 too)

You also can check credential manager.

On the other hand, you can change storage key:

var c = new VssClientCredentials();
c.Storage = new VssClientCredentialStorage(storageKind: "VssApp2", storageNamespace: "VisualStudio");
TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(u, c);