VSTS Personal access tokens with Git and Visual Studio 2017


Recently I had to deal with issue caused by the Visual Studio 2017 (15.3.3 see the version release notes) and authentication against Visual Studio Online. The Visual Studio Team Explorer that uses Git for Windows client just said
Failed to fetch from the remote repository. See the Output window for more details.
I was actually authenticated, but not authorized to fetch from the VSTS Git project repository.

Upgrade Visual Studio from 15.3.3 to 15.3.4 might fix the VSTS authentication issue

There is an open topic for TFS Launching VS with Source Control Explorer open.

I performed few steps following the topic and hoping to fix the Git for Windows issue, but with no success. There are the steps I performed, I upgraded 15.3.5 then reinstalled the git for windows client and removed the local appdata related to VS and did

1.Close all instances of VS
2.Delete %localappdata%.IdentityService
3.Restart VS

No success at the end of the exercise. So I decided to go with workaround approach to use the VSTS Personal access tokens since fetching.

Register Visual Studio Team Services Personal access token

Login to Visual Studio Team Services Project and go to your very top right of the browser where VSTS Account Setting are then select security settings.

VSTS Account Security Settings Panel

Go to Security -> Personal Access Tokens -> Add

VSTS Generate Personal Access Token

Add name and select scopes and expiry period.

VSTS Personal Access Token Scopes

Once created, make sure you copy the token now. You cannot get the value ever again.

VSTS project git pull or clone with Personal access token

Now we have to open the command prompt and open do `git pull` or `git clone` from the cmd so we get the basic authentication login from git for windows. Note: If you are prompted with windows credentials popup just close it and new command line will appear for name and password.


run cmd.exe

C:\Projects\SharePointIntranet (dev)
λ git pull
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://xxintranet.visualstudio.com': ANY_VALUE_EG_YOUR_NAME
Password for 'https://velin@xxintranet.visualstudio.com': YOUR_VSTS_PERSONAL_ACCESS_TOKEN
...

Store VSTS Git Personal access token to the Windows credentials manager

Do not close the cmd window yet, but instead store your credentials in the Windows Credentials Manager by executing the command bellow:


...

C:\Projects\SharePointIntranet (dev)
λ git config --global credential.helper manager

So next time Git for Windows will use these credentials to authenticate and they are securely stored to the Windows Credentials Manager. If you are using OS different than windows there is the Stackoverflow topic on how to cache or store git credentials

Use Visual Studio 2017 with the Git for Windows and the VSTS Personal access token

Now since we already stored the credentials, the Visual Studio 2017 Team Explorer (Git client) should pick them up by default and you should be able to use the client without any additional efforts. This is valid in case when you have only one version for the Git client or you use the Command line (CMD) with the same Git version used with the Visual Studio 2017 Team Explorer.

Windows Credentials Manager might has saved wrong or different credentials and use them instead

Make sure the Windows Credential Manager does not have any other Git credentials saved because it might use them instead of the newly created. Go to Windows -> Control Panel -> User Accounts -> Credential Manager. Under Generic Credentials delete any credentials related to git and leave only the one you saved with the `git config --global credential.helper manager` command.

Remove the Personal Access Token

Go the the Visual Studio Team Services -> Your Account -> Security -> Personal Access Tokens -> Revoke the token so it is not active anymore and then go to the windows credential manager and remove it from the Generic Credentials as well to clean the stored credential.

Conclusion

This is an alternative to authenticate against Visual Studio Team Services in times of upgrade or breaking change pushed to the Visual Studio client or the Git for Windows client as happened with me.
Posted on

Tags: Visual Studio 2017, VSTS, Visual Studio Team Services, Visual Studio Online, Git for Windows, Remote Git Repository, Personal access token

Comments