3

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,

  1. before executing teamProjects.EnsureAuthenticated(); the debugger reads PromptIfNeeded for credentials.PromptType.
  2. after the exception has been thrown and the debugger has stopped the execution, it reads 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?

riQQ
  • 9,878
  • 7
  • 49
  • 66
participant
  • 2,923
  • 2
  • 23
  • 40
  • answer link is dead. It's not pointing any sensible location – Shoter Dec 04 '17 at 15:50
  • Checkout my blogpost. Maybe it can help? http://damian.laczak.net.pl/blog/2017/09/14/tfs30063-you-are-not-authorized-to-access-possible-solution/ – Shoter Dec 04 '17 at 15:50
  • @Shoter I've fixed the link – participant Dec 04 '17 at 15:59
  • Since it was working I expect some certificate to be obsolte ... – Felix D. Dec 04 '17 at 16:00
  • Is your issue fixed once you change DoNotPrompt to PromptIfNeeded? Could you please add an answer for this case? – Cece Dong - MSFT Dec 05 '17 at 06:24
  • @CeceDong-MSFT No, the exception is thrown and when the debugger stops and I inspect the credentials the PromptType has changed from PromptIfNeeded to DoNotPrompt - still no connection to VSTS. – participant Dec 05 '17 at 07:22
  • Not sure if this is useful, but the issue is similar: We use VSTS through Vistual Studio. If we do not use VSTS for an extended period of time (a few weeks), it will prompt that we are not authorized. A workaround we found, was to open the **Team Explorer** in Visual Studio, go to **Manage Connections**, right click any of the connections to VSTS, and select **Connect** - and the issue is gone. I have no idea why this works, but maybe you can use this approach in your solution. – Lars Kristensen Jan 22 '18 at 06:25

2 Answers2

3

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?

participant
  • 2,923
  • 2
  • 23
  • 40
  • 1
    After wasting 7 days, this thread has been a savior. Using Task fixes the problem. – thepace May 31 '18 at 10:10
  • 1
    @thepace Me too :-) – participant Jun 01 '18 at 21:00
  • I've tried looking into this problem every several months or so and this is the only time it's worked for me. Thanks @participant. It's so difficult to find this question/answer too since it's got so little up votes. My guess for why it works is something to do with the UI thread not working with the Prompt. I don't know why Microsoft wouldn't have that documented though. – John Grabanski Sep 13 '18 at 20:35
2

VS has added a registry entry to store the credential, try to delete the entry in the following path:

HKEY_CURRENT_USER\Software\Microsoft\VSCommon\14.0\ClientServices\TokenStorage\VisualStudio\VssApp

Update:

Also, try the code below to see whether it works:

 var credentials = new VssClientCredentials();
 credentials.PromptType = CredentialPromptType.PromptIfNeeded;
 credentials.Storage = new VssClientCredentialStorage(storageKind: "VssApp2", storageNamespace: "VisualStudio");
 var aTeamProjects = new TfsTeamProjectCollection(new Uri("https://xxxxx.visualstudio.com/"), credentials);
 aTeamProjects.EnsureAuthenticated();
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39