none
Change Password of Active Directory User from Client Using C++ RRS feed

  • Question

  • I'm trying to connect to Active Directory using ldap in c++ over SSL (port 636).I have successfully connected my client PC with the Server PC having Active Directory on the port 389. The reason I want to connect over SSL is because I would like to change and edit the password of the users. When I try to edit the attributes like "sn" I'm able to do so but not the password. So I would like to connect my client PC with the server PC over SSL and edit the password using the ldap application in c++. The application is running on a WinCE 8.0 OS. [Server PC is Windows Server 2012.]

    Below is the code that I'm using:

    string myaarray[5];
    	wstring IUserList;
    	LDAP *ldap;
    	LDAPMessage  *entry;
    	LDAPMessage *answer = NULL;
    	BerElement *ber;
    	int  result;
    	int  auth_method = LDAP_AUTH_SIMPLE;
    	int  ldap_version = LDAP_VERSION3;
    	int lv = 0;
    	int   ldap_port = 389;
    	//TCHAR ldap_dn[128] = _T("Maestrotek.Local");
    	//TCHAR  *ldap_pw = _T("Admin@123");
    	TCHAR *base_dn = _T("DC=Maestrotek,DC=Local");
    	int  scope = LDAP_SCOPE_SUBTREE;
    	TCHAR *filter = _T("(&(objectClass=user))");
    	char *attrs[] = { "memberOf", NULL };
    	int  attrsonly = 0;
    	int  entries_found = 0;
    	PWCHAR attribute;
    	PWCHAR *values;
    	int a = LDAP_PORT;
    	//if ((ldap = ldap_init(_T("127.0.0.1"), LDAP_PORT)) == NULL) {
    	
    	if ((ldap = ldap_sslinit(_T("WIN-J48M2AB8EMI.Maestrotek.Local"), LDAP_SSL_PORT,1)) == NULL) {
    
    	}
    	else {
    	}
    	
    	result = ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
    
    	if (result != LDAP_SUCCESS) {
    
    	}
    	
    	result = ldap_set_option(ldap, LDAP_OPT_SSL, LDAP_OPT_ON);
    	result = ldap_get_option(ldap, LDAP_OPT_SSL, &lv);
    	if (result != LDAP_SUCCESS) {
    
    	}
    	
    	if ((void*)lv == LDAP_OPT_ON)
    	{
    	}
    	else
    	{
    		ldap_perror(ldap, "ldap_set_option failed!");
    	}
    	
    	TCHAR *server = _T("127.0.0.1");
    	TCHAR *username = _T("Administrator");
    	TCHAR *password = _T("Admin@123");
    	TCHAR *DomainName = _T("Maestrotek.Local");
    	TCHAR *condition = _T("displayName");
    	l_timeval time;
    	SEC_WINNT_AUTH_IDENTITY AuthId;
    
    	time.tv_sec = 30;
    	time.tv_usec = 30;
    
    	AuthId.User = (unsigned short *)username;
    	AuthId.UserLength = _tcslen(username);
    	AuthId.Password = (unsigned short *)password;
    	AuthId.PasswordLength = _tcslen(password);
    	AuthId.Domain = (unsigned short *)DomainName;
    	AuthId.DomainLength = _tcslen(DomainName);
    #ifdef UNICODE
    	AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
    #else
    	AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
    #endif
    	result = ldap_bind_s(ldap, (PWCHAR)AuthId.User, (PWCHAR)&AuthId,LDAP_AUTH_SIMPLE);
    
    	wchar_t *Password[] = { L"Rushali@123",NULL};
    	struct berval **bvalue = NULL;
    	int msgid;
    	int
    		rc = 0;
    	LDAPMod attribute2;
    	LDAPMod *list_of_attrs[2];
    	attribute2.mod_op = LDAP_MOD_REPLACE;
    	attribute2.mod_type = L"sn";
    
    
    
    	attribute2.mod_values = (PWCHAR*)Password;
            list_of_attrs[0] = &attribute2;
    	list_of_attrs[1]  = NULL;
    
    	wchar_t dnName[100] = { 0 };
    	char *dn = "CN=Rushali J. Watane,CN=Users,DC=Maestrotek,DC=Local";
    	mbstowcs(dnName, dn, 100);
    	PWCHAR dnNameval = (PWCHAR)malloc(sizeof(PWCHAR)* 20);
    	wcscpy(dnNameval, dnName);
    
    	result = ldap_modify_s(ldap, dnNameval, list_of_attrs);

    • Moved by Hart Wang Wednesday, January 3, 2018 8:38 AM
    Monday, January 1, 2018 5:42 AM

All replies

  • Hi,

    Thank you for posting here.

    I think that you post on wrong forum. Since your platform is wince 8.0 OS, you could post the issue on wince forum for better support.

    I will move the case to off-topic forum.

    Best  Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Guido Franzke Tuesday, January 2, 2018 11:33 AM
    Monday, January 1, 2018 8:10 AM