SHzr SHzr - 3 months ago 48
C# Question

Authentication to Visual Studio Team Services (was TFS Online) fails from WinForms code in VS2010

I am trying to create a standalone WinForm in VisualStudio 2010 that accesses the Team Services code base and returns the latest files into my local windows folder. However, I keep getting the following errors:

TF30063: You are not authorized to access the server.
TF30064: You are not authorized to access the server.


The only access that works is by using the default credentials, which I don't want, as they can only be used when visual studio is opened, and tfs is signed in - defeating the point of having it as a standalone winform.

Here is my code:

string teamProjectCollectionUrl = "https://xxxx.visualstudio.com/DefaultCollection";
NetworkCredential iCred = new NetworkCredential(emailUsername, password);

TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(new Uri(teamProjectCollectionUrl), iCred);
VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();
Workspace workspace = versionControlServer.GetWorkspace(sourcePath);

//For Initial Setup
WorkingFolder workfolder = new WorkingFolder(@"$\project\subproject", sourcePath);
workspace.CreateMapping(workfolder);

workspace.Get();

Answer

I ended up doing it with alternate credentials in Visual Studio 2013:

NetworkCredential netCred = new NetworkCredential("altUserName", "altPassword");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;

TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(new Uri(teamProjectCollectionUrl), tfsCred);
teamProjectCollection.Authenticate();
teamProjectCollection.EnsureAuthenticated();

VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();

Workspace workspace = versionControlServer.GetWorkspace(localPath);

// WorkingFolder workfolder = new WorkingFolder(serverPath, localPath);
// workspace.CreateMapping(workfolder);

GetStatus getStatus = workspace.Get();

See these super helpful links:

https://blogs.msdn.microsoft.com/buckh/2013/01/07/how-to-connect-to-tf-service-without-a-prompt-for-liveid-credentials/

https://blogs.msdn.microsoft.com/buckh/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer/

Comments