Authenticate to Azure resources from on-premises JavaScript apps
Article
Apps running outside of Azure (for example on-premises or at a third-party data center) should use an application service principal to authenticate to Azure when accessing Azure resources. Application service principal objects are created using the app registration process in Azure. When an application service principal is created, a client ID and client secret are generated for your app. You store the client ID, client secret, and your tenant ID in environment variables so that the Azure SDK for JavaScript uses the environment variables to authenticate your app to Azure at runtime.
A different app registration should be created for each environment (such as test, stage, production) the app is running in. This allows environment-specific resource permissions to be configured for each service principal and make sure an app deployed to one environment doesn't talk to Azure resources that are part of another environment.
1 - Register the application in Azure
An app can be registered with Azure using either the Azure portal or the Azure CLI.
Sign in to the Azure portal and follow these steps.
Instructions
Screenshot
In the Azure portal:
Enter app registrations in the search bar at the top of the Azure portal.
Select the item labeled App registrations under the under Services heading on the menu that appears below the search bar.
On the App registrations page, select + New registration.
On the Register an application page, fill out the form as follows.
Name → Enter a name for the app registration in Azure. It is recommended this name include the app name and environment (test, prod) the app registration is for.
Supported account types → Accounts in this organizational directory only.
Select Register to register your app and create the application service principal.
On the App registration page for your app:
Application (client) ID → This is the app ID that your app will use to access Azure during local development. Copy this value to a temporary location in a text editor as you'll need it in a future step.
Directory (tenant) ID → This value will also be needed by your app when it authenticates to Azure. Copy this value to a temporary location in a text editor it will also be needed it in a future step.
Client credentials → You must set the client credentials for the app before your app can authenticate to Azure and use Azure services. Select Add a certificate or secret to add credentials for your app.
On the Certificates & secrets page, select + New client secret.
The Add a client secret dialog will pop out from the right-hand side of the page. In this dialog:
Description → Enter a value of Current.
Expires → Select a value of 24 months.
Select Add to add the secret.
IMPORTANT: Set a reminder in your calendar prior to the expiration date of the secret. This way, you can add a new secret prior and update your apps prior to the expiration of this secret and avoid a service interruption in your app.
The Certificates & secrets page shows the value of the client secret.
Copy this value to a temporary location in a text editor because you need it in a future step.
IMPORTANT: This is the only time you will see this value. Once you leave or refresh this page, you won't be able to see this value again. You may add another client secret without invalidating this client secret, but you won't see this value again.
az ad sp create-for-rbac --name <app-name>
The output of the command will be similar to the following. Make note of these values or keep this window open as you'll need these values in the next step and won't be able to view the password (client secret) value again.
2 - Assign roles to the application service principal
Next, you need to determine what roles (permissions) your app needs on what resources and assign those roles to your app. Roles can be assigned a role at a resource, resource group, or subscription scope. This example will show how to assign roles for the service principal at the resource group scope since most applications group all their Azure resources into a single resource group.
Locate the resource group for your application by searching for the resource group name using the search box at the top of the Azure portal.
Navigate to your resource group by selecting the resource group name under the Resource Groups heading in the dialog box.
On the page for the resource group, select Access control (IAM) from the left-hand menu.
On the Access control (IAM) page:
Select the Role assignments tab.
Select + Add from the top menu and then Add role assignment from the resulting drop-down menu.
The Add role assignment page lists all of the roles that can be assigned for the resource group.
Use the search box to filter the list to a more manageable size. This example shows how to filter for Storage Blob roles.
Select the role that you want to assign.
Select Next to go to the next screen.
The next Add role assignment page allows you to specify what user to assign the role to.
Select User, group, or service principal under Assign access to.
Select + Select members under Members
A dialog box opens on the right-hand side of the Azure portal.
In the Select members dialog:
The Select text box can be used to filter the list of users and groups in your subscription. If needed, type the first few characters of the service principal you created for the app to filter the list.
Select the service principal associated with your application.
Select Select at the bottom of the dialog to continue.
The service principal shows as selected on the Add role assignment screen.
Select Review + assign to go to the final page and then Review + assign again to complete the process.
az role assignment create --assignee "{appId}" \
--role "{roleName}" \
--scope /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}
To get the role names that a service principal can be assigned to, use the az role definition list command.
az role definition list \
--query "sort_by([].{roleName:roleName, description:description}, &roleName)" \
--output table
For example, to allow the service principal to read, write, and delete access to Azure Storage blob containers and data to all storage accounts in the msdocs-sdk-auth-example resource group, you would assign the application service principal to the Storage Blob Data Contributor role using the following command.
az role assignment create --assignee "aaaaaaaa-bbbb-cccc-7777-888888888888" \
--role "Storage Blob Data Contributor" \
--scope /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/msdocs-javascript-sdk-auth-example \
For information on assigning permissions at the resource or subscription level using the Azure CLI, see the article Assign Azure roles using the Azure CLI.
3 - Configure environment variables for application
You must set the AZURE_CLIENT_ID, AZURE_TENANT_ID, and AZURE_CLIENT_SECRET environment variables for the process that runs your JavaScript app to make the application service principal credentials available to your app at runtime. The DefaultAzureCredential object looks for the service principal information in these environment variables.
Next, for any JavaScript code that creates an Azure SDK client object in your app, you'll want to:
Import the DefaultAzureCredential class from the @azure/identity module.
Create a DefaultAzureCredential object.
Pass the DefaultAzureCredential object to the Azure SDK client object constructor.
An example of this is shown in the following code segment.
// connect-with-default-azure-credential.js
import { BlobServiceClient } from '@azure/storage-blob';
import { DefaultAzureCredential } from '@azure/identity';
import 'dotenv/config'
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
if (!accountName) throw Error('Azure Storage accountName not found');
const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
new DefaultAzureCredential()
);
When the above code instantiates the DefaultAzureCredential object, DefaultAzureCredential reads the environment variables AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET for the application service principal information to connect to Azure with.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.