我使用了常見的模擬代碼,它工作得很好,直到我在域插入隨機「dggdgsdg」 - 它仍然工作...冒充在錯誤的域用戶不會拋出異常
if (LogonUser(Username, Domain, Password, Logon32LogonInteractive, Logon32ProviderDefault, ref existingTokenHandle) &&
DuplicateToken(existingTokenHandle, (int)SecurityImpersonationLevel.SecurityDelegation, ref duplicateTokenHandle))
{
Identity = new WindowsIdentity(duplicateTokenHandle);
ImpersonationContext = Identity.Impersonate();
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
我在我的域中使用了一些TestUser,並且它工作正常。然後,我將域名轉換爲隨機廢話「werwerhrg」,並在我的域名上模擬了TestUser!爲什麼?我期望拋出異常,爲什麼它在工作?
private const int Logon32LogonInteractive = 2;
private const int Logon32ProviderDefault = 0;
public enum SecurityImpersonationLevel
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DuplicateToken(IntPtr existingTokenHandle, int securityImpersonationLevel, ref IntPtr duplicateTokenHandle);
通常使用域認證,我看到的是用戶名/域名合併。因此,您可以指定MyDomain \ TestUser或TestUser @ MyDomain。嘗試指定你的用戶名,看看會發生什麼。 我猜你的機器是目標域的成員,並以某種方式嘗試使用該域上的用戶而不是指定的域。 – 2010-06-07 19:24:21
@Tim,我試過了,沒有運氣。如果我將'domain \ testUser'指定爲用戶名,我會收到一個異常'未知用戶名或密碼錯誤'。 – Rita 2010-06-07 19:47:49
發佈您的P/Invoke聲明。 – 2010-06-07 19:56:45