鎖定用戶根據您的Active Directory的政策,可能需要交互式登錄嘗試鎖定帳戶。您可以使用LogonUser
method of advapi32.dll來模擬這些。在我的測試中,我發現運行此循環100次並不能保證在域控制器上嘗試100次錯誤的密碼嘗試,所以您應該check the user is locked out,並在必要時進行更多嘗試。
對此的底線是您應該禁用帳戶而不是試圖鎖定它。有no functional difference between locked and disabled accounts。下面的代碼是一個黑客。
using System;
using System.Runtime.InteropServices;
namespace Test
{
class Program
{
static void Main(string[] args)
{
IntPtr token = IntPtr.Zero;
string userPrincipalName = "[email protected]";
string authority = null; // Can be null when using UPN (user principal name)
string badPassword = "bad";
int maxTries = 100;
bool res = false;
for (var i = 0; i < maxTries; i++)
{
res = LogonUser(userPrincipalName, authority, badPassword, LogonSessionType.Interactive, LogonProvider.Default, out token);
CloseHandle(token);
}
}
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string principal,
string authority,
string password,
LogonSessionType logonType,
LogonProvider logonProvider,
out IntPtr token);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
enum LogonSessionType : uint
{
Interactive = 2,
Network,
Batch,
Service,
NetworkCleartext = 8,
NewCredentials
}
enum LogonProvider : uint
{
Default = 0, // default for platform (use this!)
WinNT35, // sends smoke signals to authority
WinNT40, // uses NTLM
WinNT50 // negotiates Kerb or NTLM
}
}
}
這將禁用帳戶,這不會鎖定它。 – KoenVosters 2012-02-22 12:32:46