gpt4 book ai didi

c++ - 在 Linux 上使用 C++ 对 Active Directory 进行身份验证

转载 作者:IT王子 更新时间:2023-10-29 00:37:52 24 4
gpt4 key购买 nike

令我惊讶的是,关于这方面的示例如此之少。我基本上需要对 Active Directory 进行用户/通过身份验证。我能够初始化与事件目录的连接,但它总是给我一个“无效凭据”错误。我想知道我是否传递了错误的东西。这是我第一次尝试使用 LDAP。就此而言,我愿意接受另一个(可能有据可查的)解决方案。

#include <iostream>
#include <stdio.h>

#define LDAP_SERVER "ldap://hq.mydomain.local/"

#include <ldap.h>

int main( int argc, char** argv )
{
LDAP *ld;
int version( LDAP_VERSION3 );
int rc;
char bind_dn[100];
berval creds;
berval* serverCreds;

if( ldap_initialize( &ld, LDAP_SERVER ) ) {
std::cerr << "Error initializing ldap..." << std::endl;
return 1;
}

ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );

creds.bv_val = "password";
creds.bv_len = strlen("password");

rc = ldap_sasl_bind_s( ld, "sAMAccountName=MYDOMAIN\\UserName,dc=mydomain,dc=local", "GSSAPI", &creds, NULL, NULL, &serverCreds );

if ( rc != LDAP_SUCCESS ) {
std::cerr << "ERROR: " << ldap_err2string( rc ) << std::endl;
return 1;
} else {
std::cout << "Success." << std::endl;
}

return 0;

}

编辑:

我想确保服务器端一切正常,因此使用 ldapsearch 进行了一些测试。起初它没有用,但我终于明白了(无论如何使用 ldapsearch)。

ldapsearch -D first.last@mydomain.local -H "ldap://hq.mydomain.local:389" -b "ou=Development,ou=Domain Users,dc=mydomain,dc=local" -W "sAMAccountName=first.last"

也许不是最好的方法。对于初学者来说,关键是 -D 参数,并在末尾传递 sAMAccountName。我不会有通用名称 - 只有 Windows 登录名和密码。如果密码通过,上面的命令将向用户显示他的信息。

需要注意的是(我认为)ldap_sasl_bind_s() 不等同于设置 -D (binddn) 标志。看着this question/answer它看起来像 ldap_interactive_bind_s() 可能,但它有点复杂,因为我必须回电。

在上面的示例中,我设置了密码,但没有任何类型的 binddn/用户名,它假定我正在尝试以谁的身份进行身份验证?

最佳答案

sAMAccountName=MYDOMAIN\UserName,dc=mydomain,dc=local

此用户名格式不正确。您不需要在用户名中指定 sAMAccountName,也不需要指定 dc,除非您使用的是 Distinguished Name。您的用户名选项很少。

  1. 专有名称

CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=Com

  1. sAMaccountName

jsmith

  1. 旧版 Windows 的用户路径

"Fabrikam\jeffsmith".

  1. 用户主体名称 (UPN)

jeffsmith@Fabrikam.com

话虽如此,我不确定用户名是否是您遇到的唯一问题。我没有在本地运行你的代码。

虽然这个答案可能不会直接回答你的问题,但由于我没有在 Linux 机器上测试过这段代码,它可能会给你一个想法或让你走上正确的方向。 如果此方法仅适用于 Windows,我不会感到惊讶。

根据 MSDN您可以使用几种方法来验证用户。

The ADsOpenObject function binds to an ADSI object using explicit user name and password credentials.

此方法接受以下参数:

HRESULT ADsOpenObject(
_In_ LPCWSTR lpszPathName,
_In_ LPCWSTR lpszUserName,
_In_ LPCWSTR lpszPassword,
_In_ DWORD dwReserved,
_In_ REFIID riid,
_Out_ VOID **ppObject
);

使用此方法,您可以通过指定 usernamepassword 绑定(bind)到 Active Directory 中的对象。

如果绑定(bind)成功,返回码为S_OK,否则会得到不同的错误信息。

我不是每天都用 C++ 编写程序。我通常在 C# 世界中使用 Active DirectoryActive Directory Lightweight Services。但是我编写的这个示例代码向您展示了如何调用 ADsOpenObject 方法以使用指定的凭据绑定(bind)到 ADSI 对象。在您的情况下,只需authenticate

#include <iostream>
#include "activeds.h"

using namespace std;

int main(int argc, char* argv[])
{
HRESULT hr;
IADsContainer *pCont;
IDispatch *pDisp = NULL;
IADs *pUser;

CoInitialize(NULL);

hr = ADsOpenObject( L"LDAP://yourserver",
L"username",
L"password",
ADS_FAST_BIND, //authentication option
IID_IADs,
(void**) &pUser);


if (SUCCEEDED(hr))
{
cout << "Successfully authenticated";
}
else
cout << "Incorrect username or password";
return hr;
}

根据您的设置,您可能需要调整 ADS_AUTHENTICATION_ENUM。我建议您安装SSL 证书 并使用ADS_USE_SSL 绑定(bind)。在 AD 中处理没有 SSL 的密码可能是一场噩梦。

关于c++ - 在 Linux 上使用 C++ 对 Active Directory 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31704482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com