您可以爲本地用戶使用LOGONUSER功能登錄:
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
// types
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
// providers
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT35 = 1;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT50 = 3;
用法:
IntPtr token = IntPtr.Zero;
LogonUser("User Name", "Domain Name", "Password", LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);
using (WindowsImpersonationContext user = new WindowsIdentity(token).Impersonate())
{
// Do file operations here...
}
您的應用程序池的憑據是什麼?確保您在有權訪問您的遠程位置的帳戶下運行它 – DmitryK
我也爲應用程序池設置了相同的憑據。 –
什麼是遠程機器名稱?路徑應如下所示:\\ remotemachinename \ lo1dc \ abcd \ Admin \ Images – DmitryK