【问题标题】:How to connect to Active Directory using LDAP from C++?如何使用 C++ 中的 LDAP 连接到 Active Directory?
【发布时间】:2019-08-21 02:13:49
【问题描述】:

我在 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 服务器。

  1. 如何在此 LDAP 查询中连接到 Active Directory?我在代码中的主机名中传递服务器名还是 Active Directory 域名?

  2. 另外,我收到服务器名称未解析错误。我应该在 Windows 服务器或本地局域网中使用 dns 服务来消除错误吗?

这里是来自微软文档的代码链接:
here

【问题讨论】:

  • 请分享复制代码的 Microsoft 文档的链接。由于当前代码不完整,无法回答您的问题!
  • @Am_I_helpful 我已经发布了链接

标签: c++ active-directory ldap windows-server-2019


【解决方案1】:
  1. 如何在此 LDAP 查询中连接到 Active Directory?是在代码中的主机名中传递服务器名还是 Active Directory 域名?

根据code sample shared by you in the question,文档明确指出代码可以通过以下方式执行: (i) 将服务器名称作为命令行参数传递, (ii) 或者在没有参数的情况下执行无服务器绑定尝试。

来自Microsoft DOCS on Serverless Binding and RootDSE

如果可能,不要硬编码服务器名称。此外,在大多数 在这种情况下,绑定不应不必要地绑定到单个 服务器。 Active Directory 域服务支持无服务器绑定, 这意味着 Active Directory 可以默认绑定到 域而不指定域控制器的名称。为了 普通应用程序,这通常是登录的域 用户。对于服务应用程序,这要么是 服务登录帐户或该服务的客户端的帐户 冒充。

由于您是 Active Directory 的新手,我建议您尝试通过传递您的 AD 域名(例如 domain.local、corp.org 等)来运行代码。

  1. 另外,我收到服务器名称未解析错误。我应该在 Windows 服务器或本地局域网中使用 dns 服务来消除错误吗?

如果没有更多信息,这将很难回答。默认情况下,名称解析首先由 etc/hosts 文件完成,如果无法通过前者解析,则由 DNS 完成!您应该主要依靠后者,即正确的 DNS 设置。

您需要调查查找您提供的主机名失败的原因。您可以通过在命令提示符下检查命令nslookup yourADServerHostNamenslookup yourADServerFQDN 的输出来做一个简单的测试,并检查它是否被解析到预期的IP 地址。


注意:请确保您在执行代码的系统的网络设置中使用了正确的 DNS 服务器条目。

【讨论】:

  • 嘿,我想知道是否可以使用 LDAP HTTP 请求访问活动目录。是否可以通过 REST API 访问活动目录?我尝试搜索坚果,但没有找到具体的解释和示例代码。有什么想法吗?
  • @sham - Active Directory 使用 LDAP,并且有几个 API 可以访问不同编程语言的底层目录服务。您可以根据需要自己创建一个 REST API 建模!
猜你喜欢
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多