Skip to content

Commit a52f8d9

Browse files
committed
Create an app without a user
1 parent 683dfed commit a52f8d9

File tree

1 file changed

+74
-151
lines changed

1 file changed

+74
-151
lines changed
Lines changed: 74 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,99 @@
11
---
22
title: Create an app to access Microsoft Defender for Endpoint without a user
3-
ms.reviewer:
4-
description: Learn how to design a web app to get programmatic access to Microsoft Defender for Endpoint without a user.
3+
description: Learn how to grant a web app access to Microsoft Defender for Endpoint without a user.
54
ms.service: defender-endpoint
6-
ms.author: bagol
7-
author: batamig
8-
ms.localizationpriority: medium
9-
ms.date: 03/21/2025
10-
manager: bagol
11-
audience: ITPro
12-
ms.collection:
13-
- m365-security
14-
- tier3
15-
- must-keep
16-
ms.topic: reference
17-
ms.subservice: reference
18-
ms.custom: api
5+
ms.author: kesharab
6+
author: KesemSharabi
7+
ms.date: 09/08/2025
8+
ms.topic: how-to
9+
ms.subservice: api
10+
ms.appliesTo:
11+
- Microsoft Defender for Business and Microsoft Defender for Endpoint Plans 1 and 2
1912
search.appverid: met150
2013
---
2114

2215
# Create an app to access Microsoft Defender for Endpoint without a user
2316

24-
[!INCLUDE [Microsoft Defender XDR rebranding](../../includes/microsoft-defender.md)]
17+
When [using APIs](apis-intro.md), you might need access to Microsoft Defender for Endpoint without a user. For example, you might want to create a service that runs in the background and interacts with Defender for Endpoint on behalf of your organization. In this case, you need to create an application that can access Defender for Endpoint without a user.
2518

19+
API access requires [OAuth2.0 authentication](/azure/active-directory/develop/active-directory-v2-protocols-oauth-code).
2620

27-
**Applies to:**
21+
## Prerequisites
2822

29-
- [Microsoft Defender for Endpoint Plan 1](../microsoft-defender-endpoint.md)
30-
- [Microsoft Defender for Endpoint Plan 2](../microsoft-defender-endpoint.md)
31-
- [Microsoft Defender for Business](/defender-business)
23+
Having the [Microsoft Entra role](/entra/identity/role-based-access-control/manage-roles-portal?tabs=admin-center) for creating an app in Azure. For example, *Application Administrator*.
3224

33-
> [!IMPORTANT]
34-
> Advanced hunting capabilities are not included in Defender for Business.
35-
36-
37-
> Want to experience Microsoft Defender for Endpoint? [Sign up for a free trial.](https://go.microsoft.com/fwlink/p/?linkid=2225630)
38-
39-
[!include[Microsoft Defender for Endpoint API URIs for US Government](../../includes/microsoft-defender-api-usgov.md)]
40-
41-
[!include[Improve request performance](../../includes/improve-request-performance.md)]
42-
43-
This page describes how to create an application to get programmatic access to Defender for Endpoint without a user. If you need programmatic access to Defender for Endpoint on behalf of a user, see [Get access with user context](exposed-apis-create-app-nativeapp.md). If you are not sure which access you need, see [Get started](apis-intro.md).
44-
45-
Microsoft Defender for Endpoint exposes much of its data and actions through a set of programmatic APIs. Those APIs will help you automate work flows and innovate based on Defender for Endpoint capabilities. The API access requires OAuth2.0 authentication. For more information, see [OAuth 2.0 Authorization Code Flow](/azure/active-directory/develop/active-directory-v2-protocols-oauth-code).
46-
47-
In general, you'll need to take the following steps to use the APIs:
48-
- Create a Microsoft Entra application.
49-
- Get an access token using this application.
50-
- Use the token to access Defender for Endpoint API.
51-
52-
This article explains how to create a Microsoft Entra application, get an access token to Microsoft Defender for Endpoint, and validate the token.
53-
54-
> [!IMPORTANT]
55-
> Microsoft recommends that you use roles with the fewest permissions. This helps improve security for your organization. Global Administrator is a highly privileged role that should be limited to emergency scenarios when you can't use an existing role.
56-
57-
## Create an app
25+
## Step 1: Create an app in Azure
5826

5927
1. Sign in to the [Azure portal](https://portal.azure.com).
6028

61-
2. Navigate to **Microsoft Entra ID** \> **App registrations** \> **New registration**.
29+
2. Search for **App registrations** and navigate to *App registrations*.
6230

63-
:::image type="content" source="../media/atp-azure-new-app2.png" alt-text="The application registration pane" lightbox="../media/atp-azure-new-app2.png":::
31+
3. Select **New registration**.
6432

65-
3. In the registration form, choose a name for your application, and then select **Register**.
33+
4. Choose a name for your application, and then select **Register**.
6634

67-
4. To enable your app to access Defender for Endpoint and assign it **'Read all alerts'** permission, on your application page, select **API Permissions** \> **Add permission** \> **APIs my organization uses** >, type **WindowsDefenderATP**, and then select **WindowsDefenderATP**.
35+
5. In your application page, go to *Manage > Api Permissions > Add permission > APIs my organization uses*.
6836

69-
> [!NOTE]
70-
> `WindowsDefenderATP` does not appear in the original list. Start writing its name in the text box to see it appear.
37+
6. In the *Request API permissions* page, search for **WindowsDefenderATP** and select it.
7138

72-
:::image type="content" source="../media/add-permission.png" alt-text="The API permissions pane" lightbox="../media/add-permission.png":::
39+
7. Select the type of permissions you require, and then select **Add permissions**.
7340

74-
Select **Application permissions** \> **Alert.Read.All**, and then select **Add permissions**.
41+
* **Delegated permissions** - Sign in with your app as if you were a user.
7542

76-
:::image type="content" source="../media/application-permissions.png" alt-text="The application permission information pane" lightbox="../media/application-permissions.png":::
43+
* **Application permissions** - Access the API as a service.
7744

78-
5. Select appropriate permissions. `Read All Alerts` is only an example. Here are some examples:
45+
8. Select the appropriate permissions for your app. To determine which permission you need, look at the *Permissions* section in the API you're calling. Here are two examples:
7946

80-
- To [run advanced queries](run-advanced-query-api.md), select the `Run advanced queries` permission.
81-
- To [isolate a device](isolate-machine.md), select the `Isolate machine` permission.
82-
- To determine which permission you need, look at the **Permissions** section in the API you are interested to call.
47+
* To [run advanced queries](run-advanced-query-api.md), select *Run advanced queries*.
8348

84-
5. Select **Grant consent**.
49+
* To [isolate a device](isolate-machine.md), select *Isolate machine*.
50+
9. Select **Add permission**.
8551

86-
> [!NOTE]
87-
> Every time you add a permission, you must select **Grant consent** for the new permission to take effect.
52+
## Step 2: Add a secret to your app
8853

89-
:::image type="content" source="../media/grant-consent.png" alt-text="The grant permissions page" lightbox="../media/grant-consent.png":::
54+
1. From the application page, select *Certificates & secrets* > *New client secret*.
9055

91-
6. To add a secret to the application, select **Certificates & secrets**, add a description to the secret, and then select **Add**.
56+
2. In the *Add a client secret* pane, add a description and expiration date.
9257

93-
> [!NOTE]
94-
> After you select **Add**, select **copy the generated secret value**. You won't be able to retrieve this value after you leave.
58+
3. Select **Add**.
9559

96-
:::image type="content" source="../media/webapp-create-key2.png" alt-text="The create application option" lightbox="../media/webapp-create-key2.png":::
60+
4. Copy the **Value** of the secret you created. You won't be able to retrieve this value after you leave the page.
9761

98-
7. Write down your application ID and your tenant ID. On your application page, go to **Overview** and copy the following.
62+
5. From your app's *overview* page, copy the **Application (client) ID** and **Directory (tenant) ID**. You need this ID to authenticate your app.
9963

100-
:::image type="content" source="../media/app-and-tenant-ids.png" alt-text="The created app and tenant IDs" lightbox="../media/app-and-tenant-ids.png":::
64+
7. Write down your application ID and your tenant ID. On your application page, go to **Overview** and copy the following.
10165

102-
8. **For Microsoft Defender for Endpoint Partners only**. Set your app to be multi-tenanted (available in all tenants after consent). This is **required** for third-party apps (for example, if you create an app that is intended to run in multiple customers' tenant). This is **not required** if you create a service that you want to run in your tenant only (for example, if you create an application for your own usage that will only interact with your own data). To set your app to be multi-tenanted, follow these steps:
66+
## Multitenant apps
10367

104-
1. Go to **Authentication**, and add `https://portal.azure.com` as the **Redirect URI**.
68+
Microsoft Defender for Endpoint partners need to set their apps to be multi-tenanted. Set your app to be a multitenant app if you're planning to create an app that will run in multiple customers' tenants.
10569

106-
2. On the bottom of the page, under **Supported account types**, select the **Accounts in any organizational directory** application consent for your multi-tenant app.
70+
1. In your Azure app page, go to **Manage > Authentication**.
10771

108-
You need your application to be approved in each tenant where you intend to use it. This is because your application interacts Defender for Endpoint on behalf of your customer.
72+
2. **Add a platform**.
10973

110-
You (or your customer if you are writing a third-party app) need to select the consent link and approve your app. The consent should be done with a user who has administrative privileges in Active Directory.
74+
3. From the *Configure platforms* pane, select **Web**.
11175

112-
The consent link is formed as follows:
76+
4. Add `https://portal.azure.com` to **Redirect URIs** and select **Configure**.
11377

114-
```https
115-
https://login.microsoftonline.com/common/oauth2/authorize?prompt=consent&client_id=00000000-0000-0000-0000-000000000000&response_type=code&sso_reload=true
116-
```
78+
3. From the *Supported account types* options, select **Accounts in any organizational directory** and select **Save**.
11779

118-
Where `00000000-0000-0000-0000-000000000000` is replaced with your application ID.
80+
Once you run your app, you need it to be approved in each tenant where you intend to use it. This is because your application interacts with Defender for Endpoint on behalf of your customer. You or your customer, will need to select the consent link and approve your app. Give consent with a user who has admin privileges.
11981

82+
Here's how to form the consent link. Replace `00000000-0000-0000-0000-000000000000` with your app ID.
12083

121-
**Done!** You have successfully registered an application! See examples below for token acquisition and validation.
84+
```https
85+
https://login.microsoftonline.com/common/oauth2/authorize?prompt=consent&client_id=00000000-0000-0000-0000-000000000000&response_type=code&sso_reload=true
86+
```
12287

12388
## Get an access token
12489

125-
For more information on Microsoft Entra tokens, see the [Microsoft Entra tutorial](/azure/active-directory/develop/active-directory-v2-protocols-oauth-client-creds).
90+
This section lists a few methods for getting your app's [access token](/entra/identity-platform/v2-oauth2-client-creds-grant-flow#get-a-token).
12691

127-
### Use PowerShell
92+
# [PowerShell](#tab/PowerShell)
12893

12994
```powershell
130-
# This script acquires the App Context Token and stores it in the variable $token for later use in the script.
131-
# Paste your Tenant ID, App ID, and App Secret (App key) into the indicated quotes below.
95+
# This script acquires the App Context Token and stores it in the variable $token for later use.
96+
# Paste your Tenant ID, App ID, and App Secret (App key) into the quotes below.
13297
13398
$tenantId = '' ### Paste your tenant ID here
13499
$appId = '' ### Paste your Application ID here
@@ -147,105 +112,63 @@ $token = $authResponse.access_token
147112
$token
148113
```
149114

150-
### Use C#:
151-
152-
The following code was tested with NuGet Microsoft.Identity.Client 3.19.8.
153-
154-
> [!IMPORTANT]
155-
> The [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory) NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade, see the [migration guide](/azure/active-directory/develop/msal-migration) for more details.
156-
157-
1. Create a new console application.
158-
159-
1. Install NuGet [Microsoft.Identity.Client](https://www.nuget.org/packages/Microsoft.Identity.Client/).
115+
# [Python](#tab/Python)
160116

161-
1. Add the following:
117+
[Get token using Python](run-advanced-query-sample-python.md#get-token).
162118

163-
```csharp
164-
using Microsoft.Identity.Client;
165-
```
119+
# [Curl](#tab/Curl)
166120

167-
1. Copy and paste the following code in your app (don't forget to update the three variables: ```tenantId, appId, appSecret```):
121+
The following procedure assumes that Curl for Windows is already installed on your computer.
168122

169-
```csharp
170-
string tenantId = "00000000-0000-0000-0000-000000000000"; // Paste your own tenant ID here
171-
string appId = "11111111-1111-1111-1111-111111111111"; // Paste your own app ID here
172-
string appSecret = "22222222-2222-2222-2222-222222222222"; // Paste your own app secret here for a test, and then store it in a safe place!
173-
const string authority = "https://login.microsoftonline.com";
174-
const string audience = "https://api.securitycenter.microsoft.com";
175-
176-
IConfidentialClientApplication myApp = ConfidentialClientApplicationBuilder.Create(appId).WithClientSecret(appSecret).WithAuthority($"{authority}/{tenantId}").Build();
177-
178-
List<string> scopes = new List<string>() { $"{audience}/.default" };
179-
180-
AuthenticationResult authResult = myApp.AcquireTokenForClient(scopes).ExecuteAsync().GetAwaiter().GetResult();
181-
182-
string token = authResult.AccessToken;
183-
```
123+
1. Open a command prompt, and set `CLIENT_ID` to your Azure application ID.
184124

185-
### Use Python
125+
2. Set `CLIENT_SECRET` to your Azure application secret.
186126

187-
See [Get token using Python](run-advanced-query-sample-python.md#get-token).
127+
3. Set `TENANT_ID` to the Azure tenant ID of the customer that wants to use your app to access Defender for Endpoint.
188128

189-
### Use Curl
129+
4. Run the following command:
190130

191-
> [!NOTE]
192-
> The following procedure assumes that Curl for Windows is already installed on your computer.
131+
```console
132+
curl -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d "grant_type=client_credentials" -d "client_id=%CLIENT_ID%" -d "scope=https://securitycenter.onmicrosoft.com/windowsatpservice/.default" -d "client_secret=%CLIENT_SECRET%" "https://login.microsoftonline.com/%TENANT_ID%/oauth2/v2.0/token" -k
133+
```
193134

194-
1. Open a command prompt, and set `CLIENT_ID` to your Azure application ID.
135+
The answer resembles the following code snippet:
195136

196-
1. Set `CLIENT_SECRET` to your Azure application secret.
137+
```console
138+
{"token_type":"Bearer","expires_in":3599,"ext_expires_in":0,"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIn <truncated> aWReH7P0s0tjTBX8wGWqJUdDA"}
197139

198-
1. Set `TENANT_ID` to the Azure tenant ID of the customer that wants to use your app to access Defender for Endpoint.
199140

200-
1. Run the following command:
141+
---
201142

202-
```console
203-
curl -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d "grant_type=client_credentials" -d "client_id=%CLIENT_ID%" -d "scope=https://securitycenter.onmicrosoft.com/windowsatpservice/.default" -d "client_secret=%CLIENT_SECRET%" "https://login.microsoftonline.com/%TENANT_ID%/oauth2/v2.0/token" -k
204-
```
205-
206-
You get an answer that resembles the following code snippet:
207-
208-
```console
209-
{"token_type":"Bearer","expires_in":3599,"ext_expires_in":0,"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIn <truncated> aWReH7P0s0tjTBX8wGWqJUdDA"}
210-
```
211-
212143
## Validate the token
213144

214-
Ensure that you got the correct token:
215-
216-
1. Copy and paste the token you got in the previous step into [JWT](https://jwt.ms) in order to decode it.
145+
Follow the process in this section to ensure that you got the correct token. You can send more than one request with the same token. The token expires in an hour.
217146

218-
1. Validate that you get a roles claim with the desired permissions.
147+
1. Copy and paste [your token](#get-an-access-token) into [JWT](https://jwt.ms) to decode it.
219148

220-
In the following image, you can see a decoded token acquired from an app with permissions to all of Microsoft Defender for Endpoint's roles:
221-
222-
:::image type="content" source="../media/webapp-decoded-token.png" alt-text="The token details portion" lightbox="../media/webapp-decoded-token.png":::
149+
2. Validate that you get a roles claim with the desired permissions.
223150

224151
## Use the token to access Microsoft Defender for Endpoint API
225152

226-
1. Choose the API you want to use. For more information, see [Supported Defender for Endpoint APIs](exposed-apis-list.md).
153+
1. Choose the [API](exposed-apis-list.md) you want to use.
227154

228-
1. Set the authorization header in the `http` request you send to `Bearer {token}` (Bearer is the authorization scheme).
155+
2. Set the authorization header in the `http` request you send to `Bearer {token}`. *Bearer* is the authorization scheme.
229156

230-
1. The expiration time of the token is one hour. You can send more than one request with the same token.
157+
### Example
231158

232-
The following is an example of sending a request to get a list of alerts **using C#**:
159+
This example sends a request to get a list of alerts using C#.
233160

234161
```csharp
235162
var httpClient = new HttpClient();
236-
237163
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.securitycenter.microsoft.com/api/alerts");
238-
239164
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
240-
241165
var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
242-
243-
// Do something useful with the response
244166
```
245167

246168
## See also
247169

248-
- [Supported Microsoft Defender for Endpoint APIs](exposed-apis-list.md)
249-
- [Access Microsoft Defender for Endpoint on behalf of a user](exposed-apis-create-app-nativeapp.md)
170+
* [Get access with user context](exposed-apis-create-app-nativeapp.md)
171+
172+
* [Supported Microsoft Defender for Endpoint APIs](exposed-apis-list.md)
250173

251-
[!INCLUDE [Microsoft Defender for Endpoint Tech Community](../../includes/defender-mde-techcommunity.md)]
174+
* [Access Microsoft Defender for Endpoint on behalf of a user](exposed-apis-create-app-nativeapp.md)

0 commit comments

Comments
 (0)