Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C++;在Linux上_C++_Linux_Active Directory_Kerberos - Fatal编程技术网

C++ 使用C++;在Linux上

C++ 使用C++;在Linux上,c++,linux,active-directory,kerberos,C++,Linux,Active Directory,Kerberos,我很惊讶这方面的例子太少了。我基本上需要对Active Directory进行用户/通过身份验证。我能够初始化到active directory的连接,但它总是给我一个“无效凭据”错误。我想知道我是不是传错了。这是我第一次尝试使用LDAP。就这一点而言,我愿意接受另一种(可能有充分的文档记录)解决方案 #include <iostream> #include <stdio.h> #define LDAP_SERVER "ldap://hq.mydomain.local/

我很惊讶这方面的例子太少了。我基本上需要对Active Directory进行用户/通过身份验证。我能够初始化到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;

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

需要注意的是(我认为)ldap_sasl_bind_s()没有设置-D(binddn)标志的等效项。看起来ldap_interactive_bind_s()可能会这样,但它有点复杂,因为我必须回电话

在上面的例子中,我设置了一个密码,但没有任何类型的binddn/用户名,那么它假设我正在尝试作为谁进行身份验证

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

此用户名格式不正确。您不需要在用户名中指定
sAMAccountName
,也不需要指定
dc
,除非您使用的是
可分辨名称
。您几乎没有用户名选项

  • 可分辨名称
  • CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=Com

  • sAMaccountName
  • 犹太教

  • 以前版本Windows的用户路径
  • “Fabrikam\jeffsmith”

  • 用户主体名称(UPN)
  • jeffsmith@Fabrikam.com

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

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

    据介绍,您可以使用很少的
    方法对用户进行身份验证

    该函数使用显式用户名和密码凭据绑定到ADSI对象

    此方法接受以下参数:

    HRESULT ADsOpenObject(
      _In_  LPCWSTR lpszPathName,
      _In_  LPCWSTR lpszUserName,
      _In_  LPCWSTR lpszPassword,
      _In_  DWORD   dwReserved,
      _In_  REFIID  riid,
      _Out_ VOID    **ppObject
    );
    
    使用此方法,您可以通过指定
    用户名
    密码
    绑定到Active Directory中的对象

    如果绑定成功,则返回代码为
    S_OK
    ,否则将收到不同的错误消息

    我不会每天用
    C++
    编写程序。我通常在
    C
    世界中使用
    activedirectory
    activedirectory轻量级服务
    。但我编写的这个示例代码向您展示了如何调用
    ADsOpenObject
    方法,使用指定的凭据绑定到一个ADSI对象。在您的情况下,只需
    验证

    #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;
    }
    
    #包括
    #包括“activeds.h”
    使用名称空间std;
    int main(int argc,char*argv[])
    {
    HRESULT-hr;
    IADsContainer*pCont;
    IDispatch*pDisp=NULL;
    IADs*pUser;
    共初始化(空);
    hr=ADsOpenObject(L“LDAP://yourserver”,
    L“用户名”,
    L“密码”,
    ADS\u FAST\u BIND,//身份验证选项
    IID_IADs,
    (void**)和pUser);
    如果(成功(hr))
    {
    
    非常感谢您的帮助。当我开始在ldapsearch中使用不同的选项时,这确实让我走上了我认为的道路。请参阅我上面的编辑。此外,我认为AdsOpenObject库不适用于Linux。如果是这样,我找不到任何方法。这看起来是一种更好的方法。@kiss-o-matic,我相信您的问题是如何绑定。因为您使用的是默认端口,所以您没有使用安全。请检查此项。它解释了不同的绑定选项。如果是这种情况,它如何与ldapsearch一起工作?我指向默认端口,它工作正常。我没有看到您在ldap搜索中指定任何密码。creds结构--然后传入ldap_sasl_bind_()-第四个参数(在机制之后)。对吗?尽管我想知道它会尝试对哪个用户进行身份验证。
    #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;
    }