Logging on a Windows Domain

The Domino Server usually runs in as “SYSTEM” User.

When you write a task, that should have access to domain resources, like shared devices or printers, you have to log on manually.

The following example shows, how to do this with C/C++:

const char* sUsername, sDomain, sPassword;
HANDLE hLogon;
....
....
// Process Logon as defined user
BOOL bRet=LogonUser(sUsername,sDomain,sPassword,
LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,&hLogon);
if (bRet == FALSE) {
printf("Logon Failed");
return false;
} else {
printf("Logon successful!");
}

// Try to load Userprofile
PROFILEINFO ProfileInfo;
ZeroMemory(&ProfileInfo,sizeof(PROFILEINFO));
ProfileInfo.dwSize = sizeof(PROFILEINFO);
ProfileInfo.lpUserName = (LPSTR) (const char*) sUsername.data();
bRet = LoadUserProfile(hLogon,&ProfileInfo);
if (bRet == FALSE) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
printf((const char*)lpMsgBuf);
printf("Userprofile was not load successfull!");
return false;
}

// Impersonade this Process
bRet = ImpersonateLoggedOnUser(hLogon);
if (bRet == FALSE) {
printf("Impersonate was not successfull!");
return false;
}
....
....
CloseHandle(hLogon);

Cheers
Christian

This entry was posted in C, Windows. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s