mirror of
https://github.com/MicrosoftDocs/windows-itpro-docs.git
synced 2025-05-12 13:27:23 +00:00
Merge pull request #2145 from MicrosoftDocs/Kellylorenebaker-patch-21
Edit pass: exposed-apis-create-app-webapp.md
This commit is contained in:
commit
3f42207e10
@ -1,5 +1,5 @@
|
||||
---
|
||||
title: Create an Application to access Microsoft Defender ATP without a user
|
||||
title: Create an app to access Microsoft Defender ATP without a user
|
||||
ms.reviewer:
|
||||
description: Learn how to design a web app to get programmatic access to Microsoft Defender ATP without a user.
|
||||
keywords: apis, graph api, supported apis, actor, alerts, machine, user, domain, ip, file, advanced hunting, query
|
||||
@ -23,104 +23,88 @@ ms.topic: article
|
||||
|
||||
- Want to experience Microsoft Defender ATP? [Sign up for a free trial.](https://www.microsoft.com/microsoft-365/windows/microsoft-defender-atp?ocid=docs-wdatp-exposedapis-abovefoldlink)
|
||||
|
||||
This page describes how to create an application to get programmatic access to Microsoft Defender ATP without a user.
|
||||
|
||||
If you need programmatic access Microsoft Defender ATP 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).
|
||||
This page describes how to create an application to get programmatic access to Microsoft Defender ATP without a user. If you need programmatic access to Microsoft Defender ATP 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).
|
||||
|
||||
Microsoft Defender ATP 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 Microsoft Defender ATP capabilities. The API access requires OAuth2.0 authentication. For more information, see [OAuth 2.0 Authorization Code Flow](https://docs.microsoft.com/azure/active-directory/develop/active-directory-v2-protocols-oauth-code).
|
||||
|
||||
In general, you’ll need to take the following steps to use the APIs:
|
||||
- Create an AAD application
|
||||
- Get an access token using this application
|
||||
- Use the token to access Microsoft Defender ATP API
|
||||
- Create an Azure Active Directory (Azure AD) application.
|
||||
- Get an access token using this application.
|
||||
- Use the token to access Microsoft Defender ATP API.
|
||||
|
||||
This page explains how to create an AAD application, get an access token to Microsoft Defender ATP and validate the token.
|
||||
This article explains how to create an Azure AD application, get an access token to Microsoft Defender ATP, and validate the token.
|
||||
|
||||
## Create an app
|
||||
|
||||
1. Log on to [Azure](https://portal.azure.com) with user that has **Global Administrator** role.
|
||||
1. Log on to [Azure](https://portal.azure.com) with a user that has the **Global Administrator** role.
|
||||
|
||||
2. Navigate to **Azure Active Directory** > **App registrations** > **New registration**.
|
||||
|
||||

|
||||
|
||||
3. In the registration form, choose a name for your application and then click **Register**.
|
||||
3. In the registration form, choose a name for your application, and then select **Register**.
|
||||
|
||||
4. Allow your Application to access Microsoft Defender ATP and assign it **'Read all alerts'** permission:
|
||||
4. To enable your app to access Microsoft Defender ATP 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**.
|
||||
|
||||
- On your application page, click **API Permissions** > **Add permission** > **APIs my organization uses** > type **WindowsDefenderATP** and click on **WindowsDefenderATP**.
|
||||
|
||||
- **Note**: WindowsDefenderATP does not appear in the original list. You need to start writing its name in the text box to see it appear.
|
||||
> [!NOTE]
|
||||
> WindowsDefenderATP does not appear in the original list. You need to start writing its name in the text box to see it appear.
|
||||
|
||||

|
||||
|
||||
- Choose **Application permissions** > **Alert.Read.All** > Click on **Add permissions**
|
||||
- Select **Application permissions** > **Alert.Read.All**, and then select **Add permissions**.
|
||||
|
||||

|
||||
|
||||
**Important note**: You need to select the relevant permissions. 'Read All Alerts' is only an example!
|
||||
Note that you need to select the relevant permissions. 'Read All Alerts' is only an example. For instance:
|
||||
|
||||
For instance,
|
||||
|
||||
- To [run advanced queries](run-advanced-query-api.md), select 'Run advanced queries' permission
|
||||
- To [isolate a machine](isolate-machine.md), select 'Isolate machine' permission
|
||||
- To [run advanced queries](run-advanced-query-api.md), select the 'Run advanced queries' permission.
|
||||
- To [isolate a machine](isolate-machine.md), select the 'Isolate machine' permission.
|
||||
- To determine which permission you need, please look at the **Permissions** section in the API you are interested to call.
|
||||
|
||||
5. Click **Grant consent**
|
||||
5. Select **Grant consent**.
|
||||
|
||||
- **Note**: Every time you add permission you must click on **Grant consent** for the new permission to take effect.
|
||||
> [!NOTE]
|
||||
> Every time you add a permission, you must select **Grant consent** for the new permission to take effect.
|
||||
|
||||

|
||||

|
||||
|
||||
6. Add a secret to the application.
|
||||
6. To add a secret to the application, select **Certificates & secrets**, add a description to the secret, and then select **Add**.
|
||||
|
||||
- Click **Certificates & secrets**, add description to the secret and click **Add**.
|
||||
|
||||
**Important**: After click Add, **copy the generated secret value**. You won't be able to retrieve after you leave!
|
||||
> [!NOTE]
|
||||
> After you select **Add**, select **copy the generated secret value**. You won't be able to retrieve this value after you leave.
|
||||
|
||||

|
||||
|
||||
7. Write down your application ID and your tenant ID:
|
||||
|
||||
- On your application page, go to **Overview** and copy the following:
|
||||
7. Write down your application ID and your tenant ID. On your application page, go to **Overview** and copy the following.
|
||||
|
||||

|
||||
|
||||
8. **For Microsoft Defender ATP Partners only** - Set your application to be multi-tenanted (available in all tenants after consent)
|
||||
8. **For Microsoft Defender ATP 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:
|
||||
|
||||
This is **required** for 3rd party applications (for example, if you create an application that is intended to run in multiple customers tenant).
|
||||
- Go to **Authentication**, and add https://portal.azure.com as the **Redirect URI**.
|
||||
|
||||
This is **not required** if you create a service that you want to run in your tenant only (i.e. if you create an application for your own usage that will only interact with your own data)
|
||||
- On the bottom of the page, under **Supported account types**, select the **Accounts in any organizational directory** application consent for your multi-tenant app.
|
||||
|
||||
- Go to **Authentication** > Add https://portal.azure.com as **Redirect URI**.
|
||||
You need your application to be approved in each tenant where you intend to use it. This is because your application interacts Microsoft Defender ATP on behalf of your customer.
|
||||
|
||||
- On the bottom of the page, under **Supported account types**, mark **Accounts in any organizational directory**
|
||||
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.
|
||||
|
||||
- Application consent for your multi-tenant Application:
|
||||
|
||||
You need your application to be approved in each tenant where you intend to use it. This is because your application interacts with Microsoft Defender ATP application on behalf of your customer.
|
||||
|
||||
You (or your customer if you are writing a 3rd party application) need to click the consent link and approve your application. The consent should be done with a user who has admin privileges in the active directory.
|
||||
|
||||
Consent link is of the form:
|
||||
The consent link is formed as follows:
|
||||
|
||||
```
|
||||
https://login.microsoftonline.com/common/oauth2/authorize?prompt=consent&client_id=00000000-0000-0000-0000-000000000000&response_type=code&sso_reload=true
|
||||
```
|
||||
|
||||
where 00000000-0000-0000-0000-000000000000 should be replaced with your Application ID
|
||||
Where 00000000-0000-0000-0000-000000000000 is replaced with your application ID.
|
||||
|
||||
|
||||
- **Done!** You have successfully registered an application!
|
||||
- See examples below for token acquisition and validation.
|
||||
**Done!** You have successfully registered an application! See examples below for token acquisition and validation.
|
||||
|
||||
## Get an access token examples:
|
||||
## Get an access token
|
||||
|
||||
For more details on AAD token, refer to [AAD tutorial](https://docs.microsoft.com/azure/active-directory/develop/active-directory-v2-protocols-oauth-client-creds)
|
||||
For more details on Azure AD tokens, see the [Azure AD tutorial](https://docs.microsoft.com/azure/active-directory/develop/active-directory-v2-protocols-oauth-client-creds).
|
||||
|
||||
### Using PowerShell
|
||||
### Use PowerShell
|
||||
|
||||
```
|
||||
# That code gets the App Context Token and save it to a file named "Latest-token.txt" under the current directory
|
||||
@ -144,19 +128,19 @@ Out-File -FilePath "./Latest-token.txt" -InputObject $token
|
||||
return $token
|
||||
```
|
||||
|
||||
### Using C#:
|
||||
### Use C#:
|
||||
|
||||
>The below code was tested with Nuget Microsoft.IdentityModel.Clients.ActiveDirectory 3.19.8
|
||||
The following code was tested with Nuget Microsoft.IdentityModel.Clients.ActiveDirectory 3.19.8.
|
||||
|
||||
- Create a new Console Application
|
||||
- Install Nuget [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/)
|
||||
- Add the below using
|
||||
1. Create a new console application.
|
||||
1. Install Nuget [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/).
|
||||
1. Add the following:
|
||||
|
||||
```
|
||||
using Microsoft.IdentityModel.Clients.ActiveDirectory;
|
||||
```
|
||||
|
||||
- Copy/Paste the below code in your application (do not forget to update the 3 variables: ```tenantId, appId, appSecret```)
|
||||
1. Copy and paste the following code in your app (don't forget to update the three variables: ```tenantId, appId, appSecret```):
|
||||
|
||||
```
|
||||
string tenantId = "00000000-0000-0000-0000-000000000000"; // Paste your own tenant ID here
|
||||
@ -173,26 +157,25 @@ return $token
|
||||
```
|
||||
|
||||
|
||||
### Using Python
|
||||
### Use Python
|
||||
|
||||
Refer to [Get token using Python](run-advanced-query-sample-python.md#get-token)
|
||||
See [Get token using Python](run-advanced-query-sample-python.md#get-token).
|
||||
|
||||
### Using Curl
|
||||
### Use Curl
|
||||
|
||||
> [!NOTE]
|
||||
> The below procedure supposed Curl for Windows is already installed on your computer
|
||||
> The following procedure assumes that Curl for Windows is already installed on your computer.
|
||||
|
||||
- Open a command window
|
||||
- Set CLIENT_ID to your Azure application ID
|
||||
- Set CLIENT_SECRET to your Azure application secret
|
||||
- Set TENANT_ID to the Azure tenant ID of the customer that wants to use your application to access Microsoft Defender ATP application
|
||||
- Run the below command:
|
||||
1. Open a command prompt, and set CLIENT_ID to your Azure application ID.
|
||||
1. Set CLIENT_SECRET to your Azure application secret.
|
||||
1. Set TENANT_ID to the Azure tenant ID of the customer that wants to use your app to access Microsoft Defender ATP.
|
||||
1. Run the following command:
|
||||
|
||||
```
|
||||
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
|
||||
```
|
||||
|
||||
You will get an answer of the form:
|
||||
You will get an answer in the following form:
|
||||
|
||||
```
|
||||
{"token_type":"Bearer","expires_in":3599,"ext_expires_in":0,"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIn <truncated> aWReH7P0s0tjTBX8wGWqJUdDA"}
|
||||
@ -200,20 +183,21 @@ You will get an answer of the form:
|
||||
|
||||
## Validate the token
|
||||
|
||||
Sanity check to make sure you got a correct token:
|
||||
- Copy/paste into [JWT](https://jwt.ms) the token you get in the previous step in order to decode it
|
||||
- Validate you get a 'roles' claim with the desired permissions
|
||||
- In the screen shot below you can see a decoded token acquired from an Application with permissions to all of Microsoft Defender ATP's roles:
|
||||
Ensure that you got the correct token:
|
||||
|
||||
1. Copy and paste the token you got in the previous step into [JWT](https://jwt.ms) in order to decode it.
|
||||
1. Validate that you get a 'roles' claim with the desired permissions
|
||||
1. In the following image, you can see a decoded token acquired from an app with permissions to all of Microsoft Defender ATP's roles:
|
||||
|
||||

|
||||
|
||||
## Use the token to access Microsoft Defender ATP API
|
||||
|
||||
- Choose the API you want to use, for more information, see [Supported Microsoft Defender ATP APIs](exposed-apis-list.md)
|
||||
- Set the Authorization header in the Http request you send to "Bearer {token}" (Bearer is the Authorization scheme)
|
||||
- The Expiration time of the token is 1 hour (you can send more then one request with the same token)
|
||||
1. Choose the API you want to use. For more information, see [Supported Microsoft Defender ATP APIs](exposed-apis-list.md).
|
||||
1. Set the authorization header in the http request you send to "Bearer {token}" (Bearer is the authorization scheme).
|
||||
1. The expiration time of the token is one hour. You can send more then one request with the same token.
|
||||
|
||||
- Example of sending a request to get a list of alerts **using C#**
|
||||
The following is an example of sending a request to get a list of alerts **using C#**:
|
||||
```
|
||||
var httpClient = new HttpClient();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user