我尝试使用LogonUser方法连接到另一台计算机以保存文件。这是我的代码:
public class ImpersonationHelper
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9
}
enum LogonProvider
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3
}
public IntPtr Login(string username,string password)
{
// machineIp
string domain = "XXX.XX.XX.XX";
int logonType = (int)LogonType.Network;
int logonProvider = (int)LogonProvider.WinNT50;
IntPtr tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(username, domain, password, logonType, logonProvider, ref tokenHandle);
if (!returnValue)
{
int nErrorCode = Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(nErrorCode);
}
return tokenHandle;
}
}
LogonUser的返回值始终为false,Marshal.GetLastWin32Error()的值有时为0(表示"操作成功完成")然后是1326(表示"用户名或密码为不正确&#34)。我的错误是什么?