我使用以下代码:
HANDLE token;
if (!LogonUser("LocalUser", "InvalidDomain", "Password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token))
{
unsigned long error = GetLastError();
}
如果计算机位于域中,并且输入了无效域,则可能需要很长时间才能对登录进行身份验证。这可以是5秒到2/3分钟。
有没有办法改善这个?
答案 0 :(得分:1)
这是一个老问题,几年前我遇到了同样的问题。作为解决方法,您可以使用WaitForSingleObject。基本思想是启动一个线程,该线程将执行LogonUser并使主线程等待n毫秒,以便工作线程在完成时设置一个句柄。
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
HANDLE athEvent;
DWORD WINAPI ThreadAuth(LPVOID);
DWORD AUTHTIMEOUT = 5000;
int main(void)
{
HANDLE hThread;
DWORD dwEvent, dwThreadID;
athEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadAuth, NULL,
0, &dwThreadID);
// Wait for the thread to signal
dwEvent = WaitForSingleObject(athEvent, AUTHTIMEOUT); //5 seconds timeout
switch (dwEvent)
{
// athEvent was signaled
case WAIT_OBJECT_0:
printf("Authenticated");
break;
case WAIT_TIMEOUT:
printf("Authenticate timeout\n");
TerminateThread(hThread, 0);
break;
default:
printf("Wait error: %d\n", GetLastError());
ExitProcess(0);
}
CloseHandle(athEvent);
CloseHandle(hThread);
return 0;
}
DWORD WINAPI ThreadAuth(LPVOID lpParam)
{
UNREFERENCED_PARAMETER(lpParam);
//## Insert your logon method here
//LogonUser("LocalUser", "InvalidDomain", "Password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token)
Sleep(5000); //if >= AUTHTIMEOUT then the waitforsingleobject will expire
//##
//Signal the event so WaitForSingleObject is released
SetEvent(athEvent);
return 0;
}