|
| 1 | +--- |
| 2 | +manager: nitinme |
| 3 | +author: haileytap |
| 4 | +ms.author: haileytapia |
| 5 | +ms.service: azure-ai-search |
| 6 | +ms.topic: include |
| 7 | +ms.date: 07/08/2025 |
| 8 | +--- |
| 9 | + |
| 10 | +In this quickstart, you use role-based access control (RBAC) and Microsoft Entra ID to connect to Azure AI Search from your local system. You then use REST in Visual Studio Code to interact with your search service. |
| 11 | + |
| 12 | +We recommend keyless connections for granular permissions and identity-based authentication, which eliminate the need for hard-coded API keys in your code. However, if you prefer key-based connections, see [Connect to Azure AI Search using keys](../../search-security-api-keys.md). |
| 13 | + |
| 14 | +<!-- This quickstart is a prerequisite for other quickstarts that use Microsoft Entra ID with role assignments. --> |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | ++ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 19 | + |
| 20 | ++ An [Azure AI Search service](../../search-create-service-portal.md) in any region or tier. |
| 21 | + |
| 22 | ++ The [Azure CLI](/cli/azure/install-azure-cli) for keyless authentication with Microsoft Entra ID. |
| 23 | + |
| 24 | ++ [Visual Studio Code](https://code.visualstudio.com/) with the [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client). |
| 25 | + |
| 26 | +[!INCLUDE [Setup](./search-get-started-rbac-setup.md)] |
| 27 | + |
| 28 | +## Set up authentication |
| 29 | + |
| 30 | +Before you establish a keyless connection to your Azure AI Search service, you must use the Azure CLI to authenticate your identity and generate a Microsoft Entra ID token. You specify this token in the next section. |
| 31 | + |
| 32 | +To set up authentication: |
| 33 | + |
| 34 | +1. On your local system, open a command-line tool. |
| 35 | + |
| 36 | +1. Sign in to the subscription whose ID you obtained in [Get service information](#get-service-information). |
| 37 | + |
| 38 | + ```azurecli |
| 39 | + az login |
| 40 | + ``` |
| 41 | + |
| 42 | +1. Generate an access token. |
| 43 | + |
| 44 | + ```azurecli |
| 45 | + az account get-access-token --scope https://search.azure.com/.default --query accessToken --output tsv |
| 46 | + ``` |
| 47 | +
|
| 48 | +1. Make a note of the token. |
| 49 | +
|
| 50 | +## Connect to Azure AI Search |
| 51 | +
|
| 52 | +You can use the REST Client extension to send requests to Azure AI Search. For request authentication, include an `Authorization` header with the Microsoft Entra ID token you previously generated. |
| 53 | +
|
| 54 | +To use REST for keyless connections: |
| 55 | +
|
| 56 | +1. On your local system, open Visual Studio Code. |
| 57 | +
|
| 58 | +1. Create a `.rest` or `.http` file. |
| 59 | +
|
| 60 | +1. Paste the following placeholders into the file. |
| 61 | +
|
| 62 | + ```http |
| 63 | + @baseUrl = PUT-YOUR-SEARCH-SERVICE-ENDPOINT-HERE |
| 64 | + @token = PUT-YOUR-PERSONAL-IDENTITY-TOKEN-HERE |
| 65 | + ``` |
| 66 | + |
| 67 | +1. Replace `@baseUrl` with the value you obtained in [Get service information](#get-service-information). |
| 68 | + |
| 69 | +1. Replace `@token` with the value you obtained in [Set up authentication](#set-up-authentication). |
| 70 | + |
| 71 | +1. Make a REST call to authenticate with your token and connect to your search service. |
| 72 | + |
| 73 | + ```http |
| 74 | + POST https://{{baseUrl}}/indexes/hotels-sample-index/docs/search?api-version=2024-07-01 HTTP/1.1 |
| 75 | + Content-type: application/json |
| 76 | + Authorization: Bearer {{token}} |
| 77 | + |
| 78 | + { |
| 79 | + "queryType": "simple", |
| 80 | + "search": "beach access", |
| 81 | + "filter": "", |
| 82 | + "select": "HotelName,Description,Category,Tags", |
| 83 | + "count": true |
| 84 | + } |
| 85 | + ``` |
| 86 | + |
| 87 | +### Troubleshoot 401 errors |
| 88 | + |
| 89 | ++ Revisit [Configure role-based access](#configure-role-based-access). Your search service must have **Role-based access control** or **Both** enabled. Policies at the subscription or resource group level might also override your role assignments. |
| 90 | + |
| 91 | ++ Revisit [Set up authentication](#set-up-authentication). You must sign in to the correct subscription for your search service. |
| 92 | + |
| 93 | ++ Make sure your endpoint and token variables don't have surrounding quotes or extra spaces. |
| 94 | + |
| 95 | ++ Make sure your token doesn't have the `@` symbol in the request header. For example, if the variable is `@token`, the reference in the request should be `{{token}}`. |
| 96 | + |
| 97 | +If all else fails, restart your device to remove cached tokens and then repeat the steps in this quickstart, starting with [Set up authentication](#set-up-authentication). |
0 commit comments