本文介绍如何为 Azure Cosmos DB for MongoDB vCore 配置 Microsoft Entra ID 身份验证 。 本指南中的步骤配置现有 Azure Cosmos DB for MongoDB vCore 群集,以使用 Microsoft Entra ID 身份验证和人类标识(当前登录的帐户)或 Microsoft Entra ID 安全主体(例如托管标识)。 Microsoft Entra ID 身份验证允许使用组织的现有标识安全无缝地访问数据库。 本指南介绍设置身份验证、注册用户或服务主体以及验证配置的步骤。
创建 Azure Cosmos DB for MongoDB vCore 群集时,群集配置为默认使用本机身份验证。 若要使用 Entra ID 启用身份验证, 请启用 Entra ID 身份验证方法 ,并将 Entra ID 用户添加到 群集。
先决条件
- 现有的 Azure Cosmos DB for MongoDB (vCore) 群集。
获取 Entra ID 用户管理的唯一标识符
首先,获取用于管理群集上的 Entra ID 主体的唯一标识符。
使用 .. 获取az ad signed-in-user的详细信息。
az ad signed-in-user show
使用 az ad user show 获取另一个帐户的详细信息。
az ad user show --id kai@adventure-works.com
该命令将输出包含各种字段的 JSON 响应。
{
"@odata.context": "<https://microsoftgraph.chinacloudapi.cn/v1.0/$metadata#users/$entity>",
"businessPhones": [],
"displayName": "Kai Carter",
"givenName": "Kai",
"id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
"jobTitle": "Senior Sales Representative",
"mail": "<kai@adventure-works.com>",
"mobilePhone": null,
"officeLocation": "Redmond",
"preferredLanguage": null,
"surname": "Carter",
"userPrincipalName": "<kai@adventure-works.com>"
}
记录 id 属性的值。 此属性是主体的唯一标识符,有时称为 主体 ID。 在接下来的步骤中将使用此值。
使用唯一标识符获取友好名称
如果需要使用唯一标识符获取友好名称,请执行以下步骤。
使用 az ad user show 获取另一个帐户的详细信息。
az ad user show --id aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
该命令将输出包含各种字段的 JSON 响应。
{
"@odata.context": "<https://microsoftgraph.chinacloudapi.cn/v1.0/$metadata#users/$entity>",
"businessPhones": [],
"displayName": "Kai Carter",
"givenName": "Kai",
"id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
"jobTitle": "Senior Sales Representative",
"mail": "<kai@adventure-works.com>",
"mobilePhone": null,
"officeLocation": "Redmond",
"preferredLanguage": null,
"surname": "Carter",
"userPrincipalName": "<kai@adventure-works.com>"
}
请注意mail和displayName属性的值。
获取 Entra ID 服务主体的唯一标识符
若要在应用程序中使用托管标识或使用 MongoDB shell 或 Compass 等工具中的 Entra ID 凭据登录,需要检索 principalID 托管标识和 clientID 托管标识。
使用 GET REST API 调用获取托管标识的详细信息。 将以符号开头 $ 的变量替换为实际值。
az rest --method "GET" --url "https://management.chinacloudapi.cn/subscriptions/$subscription-id/resourcegroups/$resource-group-name/providers/microsoft.managedidentity/userassignedidentities/$managed-identity-name?api-version=2024-11-30"
该命令将输出包含各种字段的 JSON 响应。
{
"location": "eastus",
"name": "managed-identity-name",
"properties": {
"clientId": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
"isolationScope": "None",
"principalId": "cccccccc-0000-1111-2222-bbbbbbbbbbbb",
"tenantId": "dddddddd-0000-1111-2222-bbbbbbbbbbbb"
},
"tags": {},
"type": "Microsoft.ManagedIdentity/userAssignedIdentities"
}
请注意clientID和principalId在输出中的值。
- 使用
principalId 将托管标识作为 Entra ID 实体添加到群集。
- 通过
clientID 使用 MongoDB Shell 或 Compass 连接到群集,或在应用程序代码中通过 Entra ID 身份验证连接到群集。
管理群集身份验证方法
使用以下步骤更改现有群集上的身份验证方法。 然后,将映射到已登录标识的 Entra ID 用户添加到群集。 可以在群集上启用以下身份验证方法:
- 仅限原生 DocumentDB 身份验证方法
- 原生 DocumentDB 和 Microsoft Entra ID 身份验证方法
- Microsoft Entra ID 身份验证方法
重要
创建群集时,必须启用本机 DocumentDB 身份验证方法并指定本机管理用户凭据。 完成新群集的预配后,可以禁用本机的 DocumentDB 身份验证方法。
在群集边栏的 “设置”下,选择“ 身份验证”。
在 “身份验证方法” 部分中,选择 “本机 DocumentDB 和 Microsoft Entra ID” 以启用 Microsoft Entra ID 身份验证。
选择 “保存” 以确认身份验证方法更改。
注释
如果需要在群集上 禁用 本机 DocumentDB 身份验证方法,请使用 Azure CLI 或 REST API 调用。
若要在群集上启用 Microsoft Entra ID,请通过 HTTP PATCH 操作对现有群集进行更新,方法是向 MicrosoftEntraID 属性中的 allowedModes 添加 authConfig 值。
az resource patch \
--resource-group "<resource-group-name>" \
--name "<cluster-name>" \
--resource-type "Microsoft.DocumentDB/mongoClusters" \
--properties "{\"authConfig\":{\"allowedModes\":[\"MicrosoftEntraID\",\"NativeAuth\"]}}" \
--latest-include-preview
若要在群集上禁用Microsoft Entra ID 身份验证方法,请使用 HTTP PATCH 操作更新现有群集,方法是将allowedModes属性中的当前值替换为authConfig。
az resource patch \
--resource-group "<resource-group-name>" \
--name "<cluster-name>" \
--resource-type "Microsoft.DocumentDB/mongoClusters" \
--properties "{\"authConfig\":{\"allowedModes\":[\"NativeAuth\"]}}" \
--latest-include-preview
若要禁用本机 DocumentDB 身份验证方法并在群集上启用 Microsoft Entra ID,请通过 HTTP PATCH 操作更新现有群集,只需将 MicrosoftEntraID 值添加到 allowedModes 属性中的 authConfig 即可。
az resource patch \
--resource-group "<resource-group-name>" \
--name "<cluster-name>" \
--resource-type "Microsoft.DocumentDB/mongoClusters" \
--properties "{\"authConfig\":{\"allowedModes\":[\"MicrosoftEntraID\"]}}" \
--latest-include-preview
可以直接使用 Azure REST API,或者在 Azure CLI 环境中结合 az rest 一起使用。
使用此命令将 Microsoft Entra ID 身份验证方法添加到群集:
az rest \
--method "PUT" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>?api-version=2025-09-01" \
--body "{\"location\":\"<cluster-region>\",\"properties\":{\"authConfig\":{\"allowedModes\":[\"MicrosoftEntraID\",\"NativeAuth\"]}}}"
使用此命令从群集中删除 Microsoft Entra ID 身份验证方法,只启用本机 DocumentDB 身份验证方法:
az rest \
--method "PUT" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>?api-version=2025-09-01" \
--body "{\"location\":\"<cluster-region>\",\"properties\":{\"authConfig\":{\"allowedModes\":\"NativeAuth\"}}}"
使用此命令删除本机 DocumentDB 身份验证方法,并将 Microsoft Entra ID 身份验证方法添加到群集:
az rest \
--method "PUT" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>?api-version=2025-09-01" \
--body "{\"location\":\"<cluster-region>\",\"properties\":{\"authConfig\":{\"allowedModes\":[\"MicrosoftEntraID\"]}}}"
查看群集上已启用的身份验证方法
按照以下步骤查看群集上当前已启用的身份验证方法。
在群集边栏的 “设置”下,选择“ 身份验证”。
在 “身份验证方法 ”部分中,检查群集上当前已启用的身份验证方法。
使用 authConfig 从现有群集获取 az resource show 属性。
az resource show \
--resource-group "<resource-group-name>" \
--name "<cluster-name>" \
--resource-type "Microsoft.DocumentDB/mongoClusters" \
--query "properties.authConfig" \
--latest-include-preview
观察输出。 如果未配置 Microsoft Entra ID 身份验证,输出中仅包含数组 NativeAuth 中的 allowedModes 值。
{
"authConfig": {
"allowedModes": [
"NativeAuth"
] }
}
如果在群集上启用了 Microsoft Entra ID 身份验证,则输出将在 NativeAuth 数组中同时包含 MicrosoftEntraID 值和 allowedModes 值。
{
"authConfig": {
"allowedModes": [
"NativeAuth",
"MicrosotEntraID"
] }
}
使用此命令检查群集上当前已启用的身份验证方法:
az rest \
--method "GET" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>?api-version=2025-09-01"
观察输出。 如果未配置 Microsoft Entra ID 身份验证,输出中仅包含数组 NativeAuth 中的 allowedModes 值。
{
"authConfig": {
"allowedModes": [
"NativeAuth"
] }
}
如果在群集上启用了 Microsoft Entra ID 身份验证,则输出将在 NativeAuth 数组中同时包含 MicrosoftEntraID 值和 allowedModes 值。
{
"authConfig": {
"allowedModes": [
"NativeAuth",
"MicrosotEntraID"
] }
}
在群集上管理具有管理员权限的 Entra ID 用户
按照以下步骤将管理 Entra ID 用户添加到群集或从中删除管理 Entra ID 用户。
选择 启用了Microsoft Entra ID 身份验证方法的群集。
在群集边栏的 “设置”下,选择“ 身份验证”。
若要添加管理 Entra ID 用户,请按照以下步骤操作:
在 “Microsoft Entra ID 身份验证 ”部分中,选择“ +添加Microsoft Entra ID” 以打开允许向群集添加 Entra ID 用户和安全主体的侧面板。
在 “选择 Microsoft Entra ID 角色” 侧边面板中,选择一个或多个 Entra ID 用户,并通过点击 “选择” 确认您的选择。
注释
在将管理Microsoft Entra ID用户添加到群集时,其标识符将以aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb格式而非人工可读名称(例如kai@adventure-works.com)的形式添加到群集。
选择 “保存” 以确认身份验证方法更改。
若要从群集中删除管理 Entra ID 用户,请执行以下作:
获取要从群集中删除的用户的 Entra ID 标识符。
在 “Microsoft Entra ID 身份验证 ”部分中,选择用户标识符旁边的 “删除 ”,以从群集中删除该用户。
获取 需要添加到群集或从群集中删除的用户或服务主体的唯一 ID。
若要添加管理 Entra ID 用户,请使用 az resource create。 此命令将创建类型 Microsoft.DocumentDB/mongoClusters/users为的新资源。 通过将父群集的名称与您的身份的主体 ID连接起来来生成资源的名称。
az resource create \
--resource-group "<resource-group-name>" \
--name "<cluster-name>/users/<principal-id>" \
--resource-type "Microsoft.DocumentDB/mongoClusters/users" \
--location "<cluster-region>" \
--properties "{\"identityProvider\":{\"type\":\"MicrosoftEntraID\",\"properties\":{\"principalType\":\"User\"}},\"roles\":[{\"db\":\"admin\",\"role\":\"root\"}]}" \
--latest-include-preview
小窍门
例如,如果父资源已命名 example-cluster ,并且主体 ID 为 aaaaaaaa-bbbb-cccc-1111-222222222222,则资源的名称为:
"example-cluster/users/aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
此外,如果要注册服务主体(如托管标识),则会将 identityProvider.properties.principalType 属性的值替换为 ServicePrincipal。
若要删除管理 Entra ID 用户,请使用 az resource delete。 此命令删除类型为 Microsoft.DocumentDB/mongoClusters/users 的资源。 通过将父群集的名称与您的身份的主体 ID连接起来来生成资源的名称。
az resource delete \
--resource-group "<resource-group-name>" \
--name "<cluster-name>/users/<principal-id>" \
--resource-type "Microsoft.DocumentDB/mongoClusters/users" \
--latest-include-preview
获取 需要添加到群集或从群集中删除的用户或服务主体的唯一 ID。
若要将管理 Entra ID 用户添加到群集,请使用带有以下命令的 PUT Azure REST API 调用 az rest :
az rest \
--method "PUT" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>/users/<principal-id>?api-version=2025-09-01" \
--body "{\"location\":\"<cluster-region>\",\"properties\":{\"identityProvider\":{\"type\":\"MicrosoftEntraID\",\"properties\":{\"principalType\":\"User\"}},\"roles\":[{\"db\":\"admin\",\"role\":\"root\"}]}}"
小窍门
例如,如果父资源已命名 example-cluster ,并且主体 ID 为 aaaaaaaa-bbbb-cccc-1111-222222222222,则资源的名称为:
"example-cluster/users/aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
此外,如果要注册服务主体(如托管标识),则会将 identityProvider.properties.principalType 属性的值替换为 ServicePrincipal。
若要从群集中删除管理 Entra ID 用户,请使用以下命令使用 az rest DELETE Azure REST API 调用:
az rest \
--method "DELETE" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>/users/<principal-id>?api-version=2025-09-01"
查看群集上的管理型 Entra ID 用户
在群集上查看 管理用户 时,群集在预配过程中始终会创建一个本地内置的管理用户,并列出添加到群集的所有管理 Entra ID 用户。 所有管理员 Entra ID 用户都复制到数据库。
在数据库中创建非管理员 Entra ID 用户。 列出数据库中的非管理用户时,该列表包含所有管理和非管理 Entra ID 用户以及所有 辅助(非管理)本机 DocumentDB 用户。
请按以下步骤查看添加到群集的所有管理 Entra ID 用户。
使用 REST API 选项卡上的命令列出群集上的管理用户。
使用此命令列出群集上的所有管理用户:
az rest \
--method "GET" \
--url "https://management.chinacloudapi.cn/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/mongoClusters/<cluster-name>/users?api-version=2025-09-01"
观察输出。 输出包括群集上的管理用户帐户数组。 此数组包含一个内置的本机管理用户,以及所有已添加到群集的管理 Entra ID 用户和服务主体。
{
"id": "/subscriptions/<subscription-id>>/resourceGroups/<resource-group-name>>/providers/Microsoft.DocumentDB/mongoClusters/<cluser-name>>/users/<user's-unique-id>",
"name": "user's-unique-id",
"properties": {
"identityProvider": {
"properties": {
"entraTenant": "entra-tenant-id",
"principalType": "User"
},
"type": "MicrosoftEntraID"
},
...
"user": "user's-unique-id"
},
...
"type": "Microsoft.DocumentDB/mongoClusters/users"
}
注释
使用一个内置本机 DocumentDB 用户创建 Azure Cosmos DB for MongoDB vCore 群集。 完成群集预配后,可以 添加更多本机 DocumentDB 用户 。 Microsoft Entra ID 用户添加到群集后,将成为同一群集上已定义的本机 DocumentDB 用户之外的附加用户。
管理群集上非管理员权限的 Entra ID 用户
若要为非管理 Entra ID 安全主体(如用户)执行管理作,需要使用 管理 Entra ID 用户登录到群集。 可以从应用程序代码或 MongoDB shell 和 Compass 等工具执行此作。
支持针对 SecurityPrincipal 和 user 主体类型的所有非管理用户管理命令。
在 MongoDB shell 或 MongoDB Compass 中使用管理 Entra ID 用户登录到群集。
若要 在群集上添加 具有 读写 权限的非管理 Entra ID 用户,请使用 createUser 以下命令:
db.runCommand(
{
createUser:"user's-Entra-ID-identifier",
roles : [
{ role:"clusterAdmin",db:"admin" },
{ role:"readWriteAnyDatabase", db:"admin" }
],
customData:{"IdentityProvider":{"type":"MicrosoftEntraID", "properties":{"principalType":"user"}}}
}
)
若要 添加 对群集具有 只读 权限的非管理 Entra ID 用户,请使用 createUser 以下命令:
db.runCommand(
{
createUser:"user's-Entra-ID-identifier",
roles : [
{ role:"readAnyDatabase", db:"admin" }
],
customData:{"IdentityProvider":{"type":"MicrosoftEntraID", "properties":{"principalType":"user"}}}
}
)
若要从群集中删除非管理 Entra ID 用户,请使用 dropUser 命令:
db.runCommand(
{
dropUser:"user's-Entra-ID-identifier"
}
)
若要列出群集上的所有 Entra ID 和本机 DocumentDB 用户,请使用 userInfo 命令:
db.runCommand(
{
usersInfo:1
}
)
注释
所有 Entra ID 和原生 DocumentDB 管理员用户都复制到数据库。 由于这种复制,用户列表包括集群上的所有管理和非管理的 Entra ID 用户和原生 DocumentDB 用户。
连接至群集
可以使用首选语言的驱动程序中的连接 URI 或自定义设置对象连接到群集。 在任一选项中,必须将 方案 设置为 mongodb+srv 连接到群集。
主机位于*.global.mongocluster.cosmos.azure.com或*.mongocluster.cosmos.azure.com域,具体取决于使用的是当前群集还是全局读写终结点。
+srv 方案和 *.global.* 主机可确保客户端在多群集配置中动态连接到适当的可写群集,即使发生区域交换操作,也是如此。 在单群集配置中,可以不分青红皂白地使用任一连接字符串。
还必须启用 tls 设置。 其余建议的设置是最佳实践配置。
| 选项 |
价值 |
|
方案 |
mongodb+srv |
|
主机 |
<cluster-name>.global.mongocluster.cosmos.azure.com 或 <cluster-name>.mongocluster.cosmos.azure.com |
tls |
true |
authMechanism |
MONGODB-OIDC |
retrywrites |
false |
maxIdleTimeMS |
120000 |
在 Azure 门户的群集属性页上的 “设置”下,打开 连接字符串。
“连接字符串”页包含群集上启用的身份验证方法的连接字符串。 Microsoft Entra ID 连接字符串位于 “Microsoft Entra ID ”部分中。
全球
mongodb+srv://<cluster-name>.global.mongocluster.cosmos.azure.com/?tls=true&authMechanism=MONGODB-OIDC&retrywrites=false&maxIdleTimeMS=120000
群集
mongodb+srv://<cluster-name>.mongocluster.cosmos.azure.com/?tls=true&authMechanism=MONGODB-OIDC&retrywrites=false&maxIdleTimeMS=120000
const AzureIdentityTokenCallback = async (params: OIDCCallbackParams, credential: TokenCredential): Promise<OIDCResponse> => {
const tokenResponse: AccessToken | null = await credential.getToken(['https://ossrdbms-aad.database.chinacloudapi.cn/.default']);
return {
accessToken: tokenResponse?.token || '',
expiresInSeconds: (tokenResponse?.expiresOnTimestamp || 0) - Math.floor(Date.now() / 1000)
};
};
const clusterName: string = '<azure-cosmos-db-mongodb-vcore-cluster-name>';
const credential: TokenCredential = new DefaultAzureCredential();
const client = new MongoClient(
`mongodb+srv://${clusterName}.global.mongocluster.cosmos.azure.com/`, {
connectTimeoutMS: 120000,
tls: true,
retryWrites: true,
authMechanism: 'MONGODB-OIDC',
authMechanismProperties: {
OIDC_CALLBACK: (params: OIDCCallbackParams) => AzureIdentityTokenCallback(params, credential),
ALLOWED_HOSTS: ['*.azure.com']
}
}
);
class AzureIdentityTokenCallback(OIDCCallback):
def __init__(self, credential):
self.credential = credential
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
token = self.credential.get_token(
"https://ossrdbms-aad.database.chinacloudapi.cn/.default").token
return OIDCCallbackResult(access_token=token)
clusterName = "<azure-cosmos-db-mongodb-vcore-cluster-name>"
credential = DefaultAzureCredential()
authProperties = {"OIDC_CALLBACK": AzureIdentityTokenCallback(credential)}
client = MongoClient(
f"mongodb+srv://{clusterName}.global.mongocluster.cosmos.azure.com/",
connectTimeoutMS=120000,
tls=True,
retryWrites=True,
authMechanism="MONGODB-OIDC",
authMechanismProperties=authProperties
)
string tenantId = "<microsoft-entra-tenant-id>";
string clusterName = "<azure-cosmos-db-mongodb-vcore-cluster-name>";
DefaultAzureCredential credential = new();
AzureIdentityTokenHandler tokenHandler = new(credential, tenantId);
MongoUrl url = MongoUrl.Create($"mongodb+srv://{clusterName}.global.mongocluster.cosmos.azure.com/");
MongoClientSettings settings = MongoClientSettings.FromUrl(url);
settings.UseTls = true;
settings.RetryWrites = false;
settings.MaxConnectionIdleTime = TimeSpan.FromMinutes(2);
settings.Credential = MongoCredential.CreateOidcCredential(tokenHandler);
settings.Freeze();
MongoClient client = new(settings);
internal sealed class AzureIdentityTokenHandler(
TokenCredential credential,
string tenantId
) : IOidcCallback
{
private readonly string[] scopes = ["https://ossrdbms-aad.database.chinacloudapi.cn/.default"];
public OidcAccessToken GetOidcAccessToken(OidcCallbackParameters parameters, CancellationToken cancellationToken)
{
AccessToken token = credential.GetToken(
new TokenRequestContext(scopes, tenantId: tenantId),
cancellationToken
);
return new OidcAccessToken(token.Token, token.ExpiresOn - DateTimeOffset.UtcNow);
}
public async Task<OidcAccessToken> GetOidcAccessTokenAsync(OidcCallbackParameters parameters, CancellationToken cancellationToken)
{
AccessToken token = await credential.GetTokenAsync(
new TokenRequestContext(scopes, parentRequestId: null, tenantId: tenantId),
cancellationToken
);
return new OidcAccessToken(token.Token, token.ExpiresOn - DateTimeOffset.UtcNow);
}
}
将 Entra ID 与 Visual Studio Code、MongoDB shell 和 MongoDB Compass 配合使用
可以在各种工具中使用 Entra ID 身份验证,包括 Visual Studio Code 和 DocumentDB 扩展、MongoDB shell 和 MongoDB Compass 工具。 在 Visual Studio Code 中,可以使用登录到 Visual Studio Code 的当前用户向群集进行身份验证。
Azure 托管标识用于使用 Entra ID 登录到 MonogDB shell 和 Compass。 将托管标识分配给 Azure 虚拟机(VM),并使用 MongoDB shell 或 Compass 从该 VM 登录到群集。
在具有 Entra ID 身份验证的工具中执行的常见任务之一是管理群集上的辅助 Entra ID 用户。 管理 Entra ID 用户需要在 MongoDB shell、Compass 或其他 MongoDB 管理工具中进行身份验证,以便管理群集上的辅助 Entra ID 用户。
在 Visual Studio Code 中使用 Entra ID 连接到群集
若要使用 Visual Studio Code 和 DocumentDB 扩展和 Entra ID 身份验证连接到 Azure Cosmos DB for MongoDB vCore 群集,请遵循 本指南。
注释
在 Visual Studio Code 中使用 Entra ID 和 DocumentDB 扩展向 Azure Cosmos DB for MongoDB vCore 群集进行身份验证时,shell 功能不受支持。 如果需要将 MongoDB shell 与 Entra ID 身份验证配合使用,请执行以下步骤。
在 MongoDB shell 中使用 Entra ID 连接到群集
创建 用户分配的托管标识。
将托管标识分配给虚拟机。
使用托管标识元数据将托管标识作为 Entra ID 用户添加到群集。
若要连接到群集,请使用 VM 上的 MongoDB shell 中的以下连接字符串:
mongosh "mongodb+srv://<client-id>@<cluster-name>.global.mongocluster.cosmos.azure.com/?tls=true&authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:https://ossrdbms-aad.database.chinacloudapi.cn" --oidcTrustedEndpoint
其中,clientID托管标识的客户端 ID。
在 MongoDB Compass 中使用 Entra ID 连接到群集
使用以下步骤使用 Entra ID 向 MongoDB Compass 中的群集进行身份验证。
创建 用户分配的托管标识。
将托管标识分配给虚拟机。
使用托管标识元数据将托管标识作为 Entra ID 用户添加到群集。
在 VM 上运行 MongoDB Compass 。
选择+在“连接”旁边的左侧标志以添加新连接。
确保在“新建连接”窗口中启用“编辑连接字符串”切换。
将以下连接字符串粘贴到 URI 输入框中。
mongodb+srv://<client-id>@<cluster-name>.global.mongocluster.cosmos.azure.com/?tls=true&authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:https://ossrdbms-aad.database.chinacloudapi.cn
其中,clientID托管标识的客户端 ID。
打开 高级连接选项。
在“ 常规 ”选项卡上,确保 mongodb+srv 已在 “连接字符串方案”下选中。
转到“ 身份验证 ”选项卡。
确保已选择 OIDC 。
打开 OIDC 选项。
设置 目标终结点视为可信 选项。
选择“ 保存并连接”。
相关内容