แก้ไข

แชร์ผ่าน


Connect to Azure AI Search using keys

Azure AI Search supports both identity-based and key-based authentication (default) for connections to your search service.

A request made to a search service endpoint is accepted if both the request and the API key are valid and if the search service is configured to allow API keys on a request.

Important

When you create a search service, key-based authentication is the default, but it's not the most secure option. We recommend that you replace it with role-based access.

Prerequisites

You must be an Owner, Contributor, or Search Service Contributor to view or manage keys.

Enabled by default

In the Azure portal, authentication is specified on the Settings > Keys page. Options set to either API keys (default) or Both allow API keys on a request.

Screenshot of the Keys page in the Azure portal.

Types of keys

An API key is a unique string composed of 52 randomly generated numbers and letters. Visually, there's no distinction between an admin key or query key. If you lose track of what type of key is specified in your application, you can check the key values in the Azure portal.

There are two kinds of keys used for authenticating a request:

Type Permission level How it's created Maximum
Admin Full access (read-write) for all data plane (content) operations Two admin keys, primary and secondary, are generated when the service is created and can be individually regenerated on demand. Having two allows you to roll over one key while using the second key for continued access to the service. 2
Query Read-only access, scoped to the documents collection of a search index One query key is generated with the service. More can be created on demand by a search service administrator. 50

Find existing keys

You can view and manage API keys using the Azure portal, PowerShell, Azure CLI, or REST API.

  1. Sign in to the Azure portal and find your search service.

  2. From the left pane, select Settings > Keys to view admin and query keys.

    Screenshot of a portal page showing API keys.

Use keys on connections

Key-based authentication is used only for data plane (content) requests, such as creating or querying index and any other action that's performed using the Search Service REST API.

In your source code, you can directly specify the API key in a request header. Alternatively, you can store it as an environment variable or app setting in your project and then reference the variable in the request.

  • Admin keys are used for creating, modifying, or deleting objects.
  • Admin keys are also used to GET object definitions and system information, such as LIST Indexes or GET Service Statistics.
  • Query keys are typically distributed to client applications that issue queries.

Recall that key authentication is enabled by default and supports data plane operations such as indexing and queries.

However, if you disable API keys and set up role assignments, the Azure portal uses role assignments instead.

Create query keys

Query keys are used for read-only access to documents within an index for operations targeting a documents collection. Search, filter, and suggestion queries are all operations that take a query key. Any read-only operation that returns system data or object definitions, such as an index definition or indexer status, requires an admin key.

Restricting access and operations in client apps is essential to safeguarding the search assets on your service. Always use a query key rather than an admin key for any query originating from a client app.

  1. Sign in to the Azure portal and find your search service.

  2. From the left pane, select Settings > Keys to view API keys.

  3. Under Manage query keys, use the query key already generated for your service, or create new query keys. The default query key isn't named, but other generated query keys can be named for manageability.

    Screenshot of the query key management options.

Regenerate admin keys

Two admin keys are created for each service so that you can rotate a primary key while using the secondary key for business continuity.

  1. Sign in to the Azure portal and find your search service.

  2. From the left pane, select Settings > Keys.

  3. Copy the secondary key.

  4. For all applications, update the API key settings to use the secondary key.

  5. Regenerate the primary key.

  6. Update all applications to use the new primary key.

If you inadvertently regenerate both keys at the same time, all client requests using those keys will fail with HTTP 403 Forbidden. However, content isn't deleted and you aren't locked out permanently.

You can still access the service through the Azure portal or programmatically. Management functions are operative through a subscription ID not a service API key, and are thus still available even if your API keys aren't.

After you create new keys via portal or management layer, access is restored to your content (indexes, indexers, data sources, synonym maps) once you provide those keys on requests.

Migrate from keys to roles

If you want to transition to role-based access, it's helpful to understand how keys map to built-in roles in Azure AI Search:

  • An admin key corresponds to the Search Service Contributor and Search Index Data Contributor roles.
  • A query key corresponds to the Search Index Data Reader role.

Secure keys

Use role assignments to restrict access to API keys.

It's not possible to use customer-managed key encryption to encrypt API keys. Only sensitive data within the search service itself (for example, index content or connection strings in data source object definitions) can be CMK-encrypted.

  1. Sign in to the Azure portal and find your search service.

  2. From the left pane, select Access control (IAM), and then select the Role assignments tab.

  3. In the Role filter, select the roles that have permission to view or manage keys (Owner, Contributor, Search Service Contributor). The resulting security principals assigned to those roles have key permissions on your search service.

  4. As a precaution, also check the Classic administrators tab to determine whether administrators and co-administrators have access.

Best practices

  • For production workloads, switch to Microsoft Entra ID and role-based access. Alternatively, if you want to continue using API keys, be sure to always monitor who has access to your API keys and regenerate API keys on a regular cadence.

  • Only use API keys if data disclosure isn't a risk (for example, when using sample data) and if you're operating behind a firewall. Exposing API keys puts both your data and your search service at risk of unauthorized use.

  • If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.

  • Always check code, samples, and training material before publishing to make sure you don't inadvertently expose an API key.