The code below provided in this answer did work well for a while but now its throwing Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: 'TF30063: You are not authorized to access https://{mysite}.visualstudio.com/.' again.
var credentials = new VssClientCredentials();
credentials.PromptType = CredentialPromptType.PromptIfNeeded;
var teamProjects = new TfsTeamProjectCollection(tfsCollectionUri, credentials);
teamProjects.EnsureAuthenticated(); // exception thrown
Q How can I fix this problem?
Update Strange enough,
teamProjects.EnsureAuthenticated();
the debugger reads PromptIfNeeded
for credentials.PromptType
.DoNotPrompt
for credentials.PromptType
.Observation The above code works perfectly well in a console application but fails to work in a windows forms application (i.e. it throws an exception).
Q1 How can I make the above code work in a windows forms application?
If you execute the code above within a Task
(i.e. a separate thread) it just works. If the credentials are not present or stale at the location in the registry (see this answer) a window opens and you can authenticate yourself.
Can anyone explain why this works?