Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Use Defender CLI authentication to run agentic code scans locally or in CI/CD pipelines.
Authentication options
Agentic code security supports two Defender CLI authentication methods:
- App-based authentication: Use this option for CI/CD pipelines and other non-interactive automation.
- Interactive authentication: Use this option for local terminal scans by signed-in users.
Required permissions
The permissions you need depend on your authentication method. For information about assigning Defender unified RBAC permissions, see Assign permissions to users using Defender RBAC.
| Authentication method | Required permissions |
|---|---|
| App-based authentication | Application Administrator role in Microsoft Entra ID to register the app and add API permissions. Global Administrator role to grant admin consent. The app registration requires the AIScan.enabled permission to run AI scans and AIScan.Upload to upload scan results. |
| Interactive authentication | Security Administrator role in Microsoft Entra ID. In Microsoft Defender unified RBAC, assign the permissions required to run scans, upload scan results, and view or manage scan results. |
Install the Microsoft Defender Code enterprise application if required
This step is needed for both authentication methods. The script installs the Microsoft Defender Code first-party app on your tenant and grants admin consent for users running the CLI locally.
Note
For customers with an E5 license, the Microsoft Defender Code app is automatically provisioned during MDASH onboarding. Running this script isn't required. If your tenant doesn't have an E5 license, complete the following steps.
Install Azure CLI if it isn't already installed.
winget install -e --id Microsoft.AzureCLICopy the admin consent script and save it as
Grant-DefenderAdminConsent.ps1. For more information, see Microsoft Defender Code admin consent script.Run the saved script with your tenant ID.
./Grant-DefenderAdminConsent.ps1 -TenantId <tenant-id>Complete the Azure sign-in flow. Copy the code shown in Azure CLI and sign in with the redirected URL.
Verify that the application was installed:
- In Microsoft Entra ID, go to Overview.
- Search your tenant for Microsoft Defender Code.
- Confirm that the application appears under Enterprise applications.
Use app-based authentication
Use app-based authentication to run Defender CLI scans in CI/CD pipelines.
Prerequisites
- Application Administrator role in Microsoft Entra ID.
- Global Administrator role available to approve the required permissions.
- Permission to view the app registration's tenant ID and client ID, and to create or retrieve a client secret.
- Azure CLI installed.
Create a Microsoft Entra app
- Sign in to the Azure portal.
- Search for and select App registrations.
- Select New registration.
- Enter a name for your application, and then select Register.
Add API permissions
- In the new app registration, expand Manage in the left navigation pane.
- Select API permissions > Add a permission.
- Select APIs my organization uses.
- Search for and select Microsoft Defender Code.
- Select Application permissions.
- Add the permissions you need:
- To allow users to run AI scans, select AIScan.enabled.
- To allow users to upload AI scan results to Defender, select AIScan.Upload.
- Select Add permissions.
Note
The AIScan.View role currently doesn't affect permissions.
Grant admin consent
A Global Administrator must grant consent for the permissions. If the Grant admin consent button isn't available, you don't have the Global Administrator role.
- On the API permissions page, select Grant admin consent.
- Confirm when prompted.
Add a client secret
- In your app registration, select Certificates & secrets > New client secret.
- Enter a description and expiration date, and then select Add.
- Store the secret value in a secure location. You can't view it again after you leave the page.
- Record the Application (client) ID and Directory (tenant) ID from the app registration Overview page.
Configure credentials for CI/CD
To configure Defender CLI in a pipeline, add the following values as encrypted secrets in your CI/CD platform, then expose them as environment variables in the workflow or pipeline job that runs the scan:
DEFENDER_ASPM_TENANT_ID
DEFENDER_ASPM_CLIENT_ID
DEFENDER_ASPM_CLIENT_SECRET
To find DEFENDER_ASPM_TENANT_ID and DEFENDER_ASPM_CLIENT_ID, open the app registration in Microsoft Entra ID and copy the Directory (tenant) ID and Application (client) ID values from the Overview page.
To get DEFENDER_ASPM_CLIENT_SECRET, create a new client secret in the same app registration, copy the secret value immediately after it's created, and store it securely in your pipeline secret store.
Important
Don't commit this secret to source control or include it in workflow logs. Rotate secrets regularly and replace them before they expire to avoid pipeline failures.
Use interactive authentication
Use this approach to let users authenticate with their Defender credentials for local Defender CLI scans. You can also use app-based authentication instead.
Prerequisites
- At least Security Administrator role in Microsoft Entra ID.
- Required permissions assigned in Microsoft Defender unified RBAC. For more information, see Assign permissions to users using Defender RBAC.
Set the tenant environment variable
Set the tenant ID used for Defender interactive authentication in your shell as an environment variable before you sign in. This ensures Azure CLI requests the device login against the correct Microsoft Entra tenant.
DEFENDER_DFD_TENANT_ID=<tenant-id>
Sign in with Azure CLI
Run the following command to start a device code sign-in flow for Defender interactive authentication. The scope value requests the Defender interactive login permission, and the command allows sign-in even when no Azure subscription is associated with the account.
az login \
--tenant <DEFENDER_DFD_TENANT_ID> \
--scope c2fd607e-fe6e-41bd-ae58-08e2f24014aa/Defender.InteractiveLogin \
--allow-no-subscriptions \
--use-device-code
Complete the device code sign-in
- Copy the device code shown in the console, then open the device login page in your browser.
- Sign in with an account that has access to the specified tenant.
- Paste the device code when prompted and complete the authentication flow.
- Return to the console and wait for the Azure CLI session to complete.