For javascript web applications that want access to resources like Azure DevOps REST API's, they will have to support an authentication flow for their users. The Azure Active Directory Authentication Library (ADAL) for JS enables javascript application developers to setup inerative authentication flows and obtain access tokens for API usage.
This buildable sample will walk you through the steps to create a single page javascript application which uses ADAL to authenticate a user via an interactive prompt and display all projects contained in a Azure DevOps account/TFS collection.
To run this sample you will need:
- Http-server. You can download NPM http server if you need one.
- An Azure Active Directory (AAD) tenant. If you do not have one, follow these steps to set up an AAD
- A user account in your AAD tenant
- A Azure DevOps account backed by your AAD tenant where your user account has access. If you have an existing Azure DevOps account not connected to your AAD tenant follow these steps to connect your AAD tenant to your Azure DevOps account
From a shell or command line:
git clone https://github.com/Microsoft/vsts-auth-samples.git
- Sign in to the Azure Portal.
- On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
- On the left hand navigation menu, select
Azure Active Directory
. - Click on
App registrations
and selectNew application registration
from the top bar. - Enter a
name
for you application, ex. "Adal JS sample", chooseWeb app/API
forapplication type
, and enterhttp://localhost:8081
for theSign-on URL
. Finally clickcreate
at the bottom of the screen. - Save the
Application ID
from your new application registration. You will need it later in this sample. - Grant permissions for Azure DevOps. Click
Settings
->Required permissions
->add
->1 Select an API
-> type in and selectAzure DevOps (Microsoft Visual Studio Team Services)
-> check the box forDelegated Permissions
-> clickSelect
-> clickDone
-> clickGrant Permissions
-> clickYes
. - Click on
Manifest
-> set the value foroauth2AllowImplicitFlow
totrue
-> clickSave
.
- Open
index.html
in Visual Studio Code or another text editor or IDE. - Inside
index.html
there is a section calledInput Vars
you will need to update to run the sample:clientId
- (Required) update this with theapplication id
you saved from step 2.6 abovereplyUri
- (optional) In single page apps this should be the app URL itself. We have set this tohttp://localhost:8081
(where we will host our app), but if you are hosting your app at another URL please change this value and the reply URI inportal.azure.com
logoutredirectUri
- (optional) update if you are hosting your app at a location other thanhttp://localhost:8081
azureDevOpsApi
- (Required) update this with your Azure DevOps/TFS collection, e.g. http://dev.azure.com/organization/_apis/projects?api-version=4.0 for Azure DevOps or http://myserver:8080/tfs/DefaultCollection/_apis/projects?api-version=4.0 for TFS. If you would like the sample to run a different Azure DevOps API please change the entire string. Learn more about other Azure DevOps REST API'sazureDevOpsResourceId
- Do not change this value. It is used to receive Azure DevOps ADAL authentication tokens
- Navigate to the ADAL JS sample in cloned repo
vsts-auth-samples/JavascriptWebAppSample/
and start your http-server and set it to serve pages atlocalhost:8081
which will by default serveindex.html
athttp://localhost:8081
. - Navigate to
http://localhost:8081
. Sign in with a user account from your AAD tenant which has access to the VSTS account specified in theazureDevOpsApi
. All projects contained in the account should be displayed.