[英]Retrieving specific data from Active Directory via LDAP in C++ (getting lockout threshold)
[英]How to connect to Active Directory using LDAP from C++?
我已经在 Windows Server 2019 上设置了 Active Directory。我正在尝试使用 LDAP 从 Windows 客户端连接到 Active Directory。 我使用了此代码,并从 Microsoft 文档中稍作修改:
// Verify that the user passed a hostname.
if (hostname!=NULL)
{
// Convert argv[] to a wchar_t*
size_t origsize = strlen(argv[1]) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(convertedChars, wcstring, origsize, argv[1], _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
hostName = wcstring;
}
else
{
hostName = NULL;
}
// Initialize a session. LDAP_PORT is the default port, 389.
pLdapConnection = ldap_init(hostName, LDAP_PORT);
if (pLdapConnection == NULL)
{
// Set the HRESULT based on the Windows error code.
char hr = HRESULT_FROM_WIN32(GetLastError());
printf( "ldap_init failed with 0x%x.\n",hr);
goto error_exit;
}
else
printf("ldap_init succeeded \n");
// Set the version to 3.0 (default is 2.0).
returnCode = ldap_set_option(pLdapConnection,
LDAP_OPT_PROTOCOL_VERSION,
(void*)&version);
if(returnCode == LDAP_SUCCESS)
printf("ldap_set_option succeeded - version set to 3\n");
else
{
printf("SetOption Error:%0X\n", returnCode);
goto error_exit;
}
// Connect to the server.
connectSuccess = ldap_connect(pLdapConnection, NULL);
if(connectSuccess == LDAP_SUCCESS)
printf("ldap_connect succeeded \n");
else
{
printf("ldap_connect failed with 0x%x.\n",connectSuccess);
goto error_exit;
}
// Bind with current credentials (login credentials). Be
// aware that the password itself is never sent over the
// network, and encryption is not used.
printf("Binding ...\n");
returnCode = ldap_bind_s(pLdapConnection, NULL, NULL,
LDAP_AUTH_NEGOTIATE);
if (returnCode == LDAP_SUCCESS)
printf("The bind was successful");
else
goto error_exit;
// Normal cleanup and exit.
ldap_unbind(pLdapConnection);
return 0;
// On error cleanup and exit.
error_exit:
ldap_unbind(pLdapConnection);
return -1;
我是 Active Directory 的新手,以前从未使用过 Windows 服务器。
如何在此 LDAP 查询中连接到 Active Directory? 我在代码中的主机名中传递服务器名称还是 Active Directory 域名?
此外,我收到服务器名称未解析错误。 我应该在 Windows 服务器或本地局域网中使用 dns 服务来消除错误吗?
这是微软文档中代码的链接:
这里
- 如何在此 LDAP 查询中连接到 Active Directory? 我在代码中的主机名中传递服务器名称还是 Active Directory 域名?
根据您在问题中共享的代码示例,文档明确指出可以通过以下方式执行代码:(i) 将服务器名称作为命令行参数传递,(ii) 或者在没有参数的情况下无服务器绑定进行尝试。
来自Microsoft DOCS on Serverless Binding 和 RootDSE :
如果可能,不要硬编码服务器名称。 此外,在大多数情况下,绑定不应不必要地绑定到单个服务器。 Active Directory 域服务支持无服务器绑定,这意味着 Active Directory 可以绑定到默认域上,而无需指定域控制器的名称。 对于普通应用程序,这通常是登录用户的域。 对于服务应用程序,这是服务登录帐户的域或服务模拟的客户端的域。
由于您是 Active Directory 的新手,我建议您尝试通过传递您的 AD 域名(例如 domain.local、corp.org 等)来运行代码。
- 此外,我收到服务器名称未解析错误。 我应该在 Windows 服务器或本地局域网中使用 dns 服务来消除错误吗?
如果没有更多信息,这将很难回答。 默认情况下,名称解析首先由 etc/hosts 文件完成,如果无法通过前者进行解析,则由 DNS 完成! 您应该主要依赖后者,即正确的 DNS 设置。
您需要调查您提供的主机名的查找失败的原因。 您可以通过在命令提示符下检查命令nslookup yourADServerHostName
或nslookup yourADServerFQDN
的输出来做一个简单的测试,并检查它是否被解析为预期的 IP 地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.