Azure DevOps Server: a valid personal access token gets 401 because IIS Basic Authentication is on
If a personal access token (PAT) is correct, not expired and has the scope, and an on-premises Azure DevOps Server or TFS still answers 401, look at IIS before you make another token. Microsoft documents that when IIS Basic Authentication is enabled for the server's web site, PATs stop working as a way to sign in, and tells administrators to keep it turned off. Test with one request, then ask the person who runs the server to turn Basic Authentication off for that site; Git has a workaround with an extra header while it stays on.
What Microsoft says
From the page IIS Basic Authentication invalidates personal access tokens (the TFS 2018 version, in English): "We recommend that you keep IIS Basic Authentication turned off always. Only if necessary should you enable IIS Basic Authentication. When IIS Basic Authentication is enabled on your windows machine, it prevents you from using personal access tokens (PATs) as an authentication mechanism." Its example is a third-party app that signs in with a PAT and then fails. The current PAT page repeats it in the FAQ "Why did my PAT stop working?": "For Azure DevOps Server, IIS Basic Authentication prevents PAT authentication. Keep IIS Basic Authentication disabled." Microsoft's list of other causes is the usual one: the PAT expired, was revoked, became inactive, has insufficient scopes, or belongs to a user whose permissions changed.
The plugin sees the same 401: Azure DevOps Pull Requests, a plugin for JetBrains IDEs, signs in to Azure DevOps Server 2019 or later and TFS 2018 Update 2 or later with a PAT and says when the server rejected it.
Test the token with one request
Azure DevOps accepts a PAT as an HTTP Basic header with an empty user name (Use personal access tokens). Put the token in an environment variable first so it does not land in your shell history. Linux and macOS:
read -s AZDO_PAT
curl -s -o /dev/null -w "%{http_code}\n" --user ":$AZDO_PAT" "https://tfs.example.com/tfs/DefaultCollection/_apis/projects?api-version=4.1"
Windows PowerShell:
$env:AZDO_PAT = Read-Host "PAT"
$h = @{ Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$env:AZDO_PAT")) }
Invoke-WebRequest -UseBasicParsing -Headers $h "https://tfs.example.com/tfs/DefaultCollection/_apis/projects?api-version=4.1" | Select-Object StatusCode
Use your server's address, its virtual directory (often /tfs) and your collection. Code 200 means the token works and your problem is elsewhere. Code 401 with a token you have just made for this collection is the case on this page, unless the account is not allowed into the collection at all. api-version=4.1 is understood by TFS 2018 Update 2 and every newer server.
Look at IIS
Ask the administrator of the server to open Internet Information Services (IIS) Manager, select the Azure DevOps Server web site, and double-click Authentication. If Basic Authentication is Enabled, that is the cause. The same check from an elevated command prompt on the server; the site name comes from the first command:
%windir%\system32\inetsrv\appcmd.exe list sites %windir%\system32\inetsrv\appcmd.exe list config "<site name>" -section:system.webServer/security/authentication/basicAuthentication
Turning it off is the same command Microsoft's IIS reference shows for turning it on, with False (basicAuthentication):
%windir%\system32\inetsrv\appcmd.exe set config "<site name>" -section:system.webServer/security/authentication/basicAuthentication /enabled:"False" /commit:apphost
Anything that signs in with a user name and password through Basic stops working when you do this, so find those first; Windows authentication is not touched by the command.
Git while Basic stays on
Microsoft's page warns: "If you use Git with IIS Basic Authentication, Git breaks because it requires PATs for user authentication." The workaround it gives is an extra header on every Git command, with the Base64 of "user:PAT":
git -c http.extraheader="Authorization: Basic <Base64 of user:PAT>" ls-remote https://tfs.example.com/tfs/DefaultCollection/_git/Project
The header holds your token, so do not write it into .git/config or a script; Microsoft's PAT page says not to embed PATs in Git remote URLs, .git/config or source. If Git instead fails on NTLM, that is a different problem with its own fix: "NTLM authentication has been disabled in Git by default".
In the IDE
Azure DevOps Pull Requests, a plugin by Lesstep for JetBrains IDEs 2024.1 or later and Android Studio, signs in to Azure DevOps Server 2019 or later and TFS 2018 Update 2 or later with the personal access token you set in Settings | Tools | Azure DevOps Pull Requests, sent as a Basic header with an empty user name, the form Microsoft describes. When the server answers 401, the plugin says "The server rejected the personal access token: it is expired or revoked, was made in another collection, or does not have the Code (Read) scope" and asks for a new one for this collection. It does not know about IIS settings, so if a new token for the right collection fails the same way, use this page. The settings also have a "Server user name" field for servers that allow Basic authentication with your account: with a user name there, the token field holds your password, and a refusal reads "The server rejected the user name and password". That is the sign-in this page's cause leaves working, while PATs stop. Reading is free; voting, commenting and completing are Pro, with a 30-day trial.
Get it on JetBrains Marketplace
Azure DevOps, TFS and IIS are trademarks of Microsoft Corporation; IntelliJ IDEA and Rider are trademarks of JetBrains s.r.o. This page is not affiliated with or endorsed by Microsoft or JetBrains.
More: Git "NTLM authentication has been disabled" with Azure DevOps Server · Azure DevOps Server and TFS support dates · Git "Authentication failed" · 日本語
Sources
- IIS Basic Authentication invalidates personal access tokens (TFS 2018 version, Microsoft Learn)
- Use personal access tokens (Microsoft Learn): the Basic header and the FAQ on IIS
- Basic Authentication <basicAuthentication> (IIS reference, Microsoft Learn)
- A public report of "Invalid Azure URL or Personal Access Token" against Azure DevOps Server 2020 (SonarSource Community)