2015-11-06 57 views
0

我正在嘗試編寫一個C#應用程序來模擬客戶端在服務器上執行某些LDAP請求。模擬服務器上的Windows客戶端

客戶端將duplicateToken發送到服務器,服務器應該使用它來模擬客戶端。 在服務器端它不起作用,得到getLastWin32Eror:203

客戶端和服務器應用程序當前運行在同一臺PC上,但將來情況不會如此。

在客戶端:

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); 
[DllImport("advapi32.dll")] 
public static extern bool ImpersonateLoggedOnUser(IntPtr phToken); 

/// {...} 
IntPtr duplicateToken = IntPtr.Zero; 
IntPtr currentToken = WindowsIdentity.GetCurrent(TokenAccessLevels.Duplicate).Token; 
DuplicateToken(currentToken, 3, ref duplicateToken); 
//if I try ImpersonateLoggedOnUser(duplicateToken) here it works 
//Send the duplicateToken to the Server 

在服務器上:

/// {...} 
//get the clientToken from the Client 
bool isValid = ImpersonateLoggedOnUser(duplicateToken); 
if (!isValid) 
{ 
    int winerr = System.Runtime.InteropServices.Marshal.GetHRForLastWin32Error(); 
    int winerr2 = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); 
} 

服務器和客戶端不在同一個線程中運行。 你知道我是否必須將令牌附加到一個新的線程來模擬?或者有沒有辦法使用這個令牌來充當客戶。

我只需要模擬器發送一些LDAP請求作爲此特定的模擬用戶。

編輯:我更新的代碼調用ImpersonateLoggedOnUser函數來獲得最後一個錯誤:

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern bool ImpersonateLoggedOnUser(IntPtr phToken); 

現在呼籲getLastWin32Eror返回錯誤6.

+0

那麼錯誤是什麼? –

+0

已更新@BrianDesmond –

回答

0

我通過了代表後處理對服務器的IntPtr轉換它有一個字符串。 這適用於客戶端,但在另一個進程中運行的服務器不能使用TokenHandle ...