This sample shows how to generate a Personal Access Token (PAT) using the Client Libraries. You cannot use a PAT to generate a PAT and so this sample makes use of the Azure Active Directory Authentication Library (ADAL) to acquire an AAD token which is then used as a credential to authenticate requests to Azure DevOps. This sample also uses the UserPasswordCredential rather than relying on the interactive pop-up dialog - this can be useful in scenarios where you need to generate a PAT associated with an account that does not have interactive login rights.
This sample shows the basic flow for calling ADAL, and then using the resulting token as credentials to call the CreateSessionToken
method on the DelegatedAuthorizationHttpClient
class. In order to run this application you will need to register your own AAD application.
From a shell or command line:
git clone https://github.com/Microsoft/vsts-auth-samples.git
- Navigate to the sample in cloned repo
vsts-auth-samples/NonInteractivePatGenerationSample/
- Use Nuget package restore to ensure you have all dependencies installed
- Open the solution file
NonInteractivePatGenerationSample.sln
in Visual Studio 2017 - Open CS file
Program.cs
and look at the first few lines in theMain
method. Replace the username and password with the appropriate values. You will need to get theaadApplicationID
value by registering your app in AAD. - Build and run the solution, the generate PAT will be output to the console window.