I'm upgrading a C# app that talks to TFS/VSTS to use the latest TeamFoundation sdk.
I would like to connect and have the app prompt for credentials in the same way that Visual Studio does if you use that to connect to TFS.
I've downloaded the latest stable VSTS Api from nuget.org which is:
microsoft.teamfoundationserver.extendedclient.15.112.1.nupkg
I also reference assemblies it uses from my VS2017 install, here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer.
I've tried a number of combinations, but can't get it to prompt. My current code looks like this:
static void Main(string[] args)
{
try
{
var netCred = new NetworkCredential();
var basicCred = new VssBasicCredential(netCred);
var vssCred = new VssCredentials(basicCred);
vssCred.PromptType = CredentialPromptType.PromptIfNeeded;
var server = new TfsTeamProjectCollection(new Uri(serverName), vssCred);
server.Authenticate();
}
catch( Exception ex )
{
System.Console.WriteLine(ex.ToString());
}
System.Console.ReadKey();
}
It doesn't prompt, and instead outputs this exception:
Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: TF30063: You are not authorized to access https://.visualstudio.com/. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequestAndGetResponse(HttpWebRequest webRequest, WebException& webException) --- End of inner exception stack trace --- at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequest() at Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage message, TimeSpan timeout) at Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation operation, Object[] parameters, TimeSpan timeout, Object[]& outputs)
at Microsoft.TeamFoundation.Framework.Client.LocationWebService.Connect(Int32 connectOptions, Int32 lastChangeId, Int32 features) at Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Connect(ConnectOptions connectOptions) at Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Authenticate() at Microsoft.TeamFoundation.Client.TfsConnection.Authenticate() at VstsAuthTest.Program.Main(String[] args) in S:\VstsAuthTest\Program.cs:line 26
How do I get it to prompt for and cache credentials?
The old version of the TeamFoundation sdk dlls I was using seemed to work ok. The reason I'm upgrading is because the C# app seems to refuse to connect to TFS when installed on a machine with only VS2017 and not VS2015. I was hopeful upgrading to the latest SDK dlls might help solve the connection issue.
I've seen this, but it seems out of date and uses classes that are now deprecated. It's also about connecting without a prompt, but the comments include some discussion of how to get a prompt.
I've also seen these samples which appear recent, but which also use deprecated apis.
https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/samples