Overview
MongoDB支持多种可用于对应用程序进行身份验证的机制。 本页包含演示每种机制的代码示例。
提示
要学习;了解有关此页面上显示的任何机制的更多信息,请参阅每个部分中提供的链接。
要使用此页面中的身份验证示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 确保将代码示例中的所有占位符(例如<hostname> )替换为MongoDB 部署的相关值。
示例应用程序
您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:
确保已安装C驱动程序。
复制以下代码并将其粘贴到新的
.c文件中。从此页面复制代码示例,并将其粘贴到文件中的指定行。
<mongoc/mongoc.h> <bson/bson.h>
int main(void) {
mongoc_uri_t* uri = NULL; mongoc_client_t *client = NULL; mongoc_database_t *database = NULL; bson_t *ping = NULL, reply = BSON_INITIALIZER; bson_error_t error;
mongoc_init();
// Start example code here
// End example code here
if (!client) { fprintf(stderr, "client initialization failure\n"); goto cleanup; }
database = mongoc_client_get_database(client, "admin"); ping = BCON_NEW("ping", BCON_INT32(1)); if (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) { fprintf(stderr, "%s\n", error.message); goto cleanup; } printf("Pinged your deployment. You successfully connected to MongoDB!\n");
cleanup: bson_destroy(&reply); bson_destroy(ping); mongoc_database_destroy(database); mongoc_client_destroy(client); mongoc_uri_destroy(uri); mongoc_cleanup();}SCRAM-SHA-256
以下代码展示了如何使用SCRAM-SHA-256身份验证机制进行身份验证:
const char *uri = "mongodb://<percentEncodedUsername>:<percentEncodedPassword>@<hostname>:<port>/?authMechanism=SCRAM-SHA-256&authSource=<authenticationDatabase>";mongoc_client_t *client = mongoc_client_new(uri);要了解有关 SCRAM-SHA- 256身份验证的更多信息,请参阅身份验证指南中的SCRAM-SHA- 256 。
SCRAM-SHA-1
以下代码展示了如何使用SCRAM-SHA-1身份验证机制进行身份验证:
const char *uri = "mongodb://<percentEncodedUsername>:<percentEncodedPassword>@<hostname>:<port>/?authMechanism=SCRAM-SHA-1&authSource=<authenticationDatabase>";mongoc_client_t *client = mongoc_client_new(uri);要了解有关 SCRAM-SHA- 1身份验证的更多信息,请参阅身份验证指南中的SCRAM-SHA- 1 。
MONGODB X.509
以下代码演示如何创建连接 URI,以使用X.509身份验证机制进行身份验证:
mongoc_client_t *client;mongoc_ssl_opt_t ssl_opts = {0};
ssl_opts.pem_file = "mycert.pem";
const char *uri = "mongodb://<percentEncodedUsername>@<hostname>:<port>/?authMechanism=MONGODB-X509";mongoc_client_t *client = mongoc_client_new(uri);mongoc_client_set_ssl_opts(client, &ssl_opts);要学习;了解有关 X. 509身份验证的更多信息,请参阅身份验证指南中的MONGODB-X 509 。
MONGODB-AWS
以下部分介绍如何使用 MONGODB-AWS身份验证机制连接到MongoDB。当您使用 MONGODB-AWS 机制时, C驱动程序会尝试按列出的顺序从以下源检索您的 AWS凭证:
传递给连接 URI 的命名参数
环境变量
Amazon Web Services EKS AssumeRoleWithWebIdentity请求
ECS容器元数据
EC 2实例元数据
每节介绍从连接 URI 或备用外部源检索Amazon Web Services凭证时如何使用 MONGODB-AWS 进行身份验证。
要学习;了解有关使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的MONGODB- Amazon Web Services 。
连接 URI
以下代码演示如何在连接 URI 中传递Amazon Web Services凭证,以便使用 MONGODB-AWS 进行身份验证:
const char *uri = "mongodb://<awsIamAccessKeyId>:<awsIamSecretAccessKey>@<hostname>:<port>/?authMechanism=MONGODB-AWS";mongoc_client_t *client = mongoc_client_new(uri);要学习;了解通过检索连接 URI凭证来使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的连接 URI。
外部档案
以下代码展示了从环境变量、ECS元数据或 EC2实例元数据获取凭证时如何使用 MONGODB-AWS 进行身份验证:
const char *uri = "mongodb://<hostname>:<port>/?authMechanism=MONGODB-AWS";mongoc_client_t *client = mongoc_client_new(uri);要学习;了解有关通过获取外部凭证来使用Amazon Web Services进行身份验证的更多信息,请参阅身份验证指南中的以下部分: