Home > Enterprise >  couldnt connect to active directory on windows 2019 server
couldnt connect to active directory on windows 2019 server

Time:10-31

I am working on active directory and was reading this https://learn.microsoft.com/en-us/windows/win32/adsi/setting-up-c---for-adsi-development and just used the mentioned code to connect to it but its not connecting to the server and says proccess exited with status code zero

i used the below code.

#include "stdafx.h"
#include "activeds.h"

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

     // Initialize COM before calling any ADSI functions or interfaces.
     CoInitialize(NULL);

    hr = ADsGetObject( L"LDAP://CN=users,DC=fabrikam,DC=com", 
                               IID_IADsContainer, 
                               (void**) &pCont );

    if ( !SUCCEEDED(hr) )
    {
        return 0;
    }


}

what am i doing wrong i did exactly as the documentation told

CodePudding user response:

ADsGetObject is for non authenticated connection that is the code should be performed inside the server. if you are trying to connect to a server hosted on seperate machine you should be using ADsOpenObject you may have the reference in the following link https://learn.microsoft.com/en-us/windows/win32/api/adshlp/nf-adshlp-adsopenobject ADsOpenObject binds using explicit username and password you can try the following

int login(LPCWSTR uname, LPCWSTR pass)
{

    HRESULT hr;
    IADsContainer* pCont;
    IDispatch* pDisp = NULL;
    IADs* pUser;

    // Initialize COM before calling any ADSI functions or interfaces.
    CoInitialize(NULL);

    hr = ADsOpenObject(L"LDAP://machinename.domaincontroller.domain/CN=Users,DC=domaincontrollername,DC=domainname", uname, pass,
        ADS_SECURE_AUTHENTICATION, // For secure authentication
        IID_IADsContainer,
        (void**)&pCont);
    std::cout << hr << std::endl;
    std::string message = std::system_category().message(hr);
    std::cout << message << std::endl;
    if (!SUCCEEDED(hr)) {
        return 0;
    }
    else {
        return 1;
    }
}

instead of the variable you can type your username and password like L"usernameexample"

  • Related