问题
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,
- before executing
teamProjects.EnsureAuthenticated();
the debugger readsPromptIfNeeded
forcredentials.PromptType
. - after the exception has been thrown and the debugger has stopped the execution, it reads
DoNotPrompt
forcredentials.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?
回答1:
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?
回答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();
来源:https://stackoverflow.com/questions/47637033/tf30063-you-are-not-authorized-programmatic-access-not-working