背景:我有了從文件的網絡驅動器上讀取(Z :)網絡驅動器上訪問文件
這工作在我的辦公室域偉大的應用程序,但它不工作的網站(在不同的域中)。據我所知,域用戶和網絡驅動器的設置方式相同,但我無法訪問客戶域中的用戶等。
當我無法訪問網絡驅動器時,我想我需要一個令牌給用戶。這是我impersionate用戶:
[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);
...
const string userName = "USER";
const string pass = "PASS";
const string domainName = "VALIDDOMAIN.local" //tried with valid domain name and with null, same result
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
IntPtr tokenHandle = new IntPtr(0);
bool returnValue = LogonUser(userName, domainName, pass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (!returnValue)
throw new Exception("Logon failed.");
WindowsImpersonationContext impersonatedUser = null;
try
{
WindowsIdentity wid = new WindowsIdentity(tokenHandle);
impersonatedUser = wid.Impersonate();
}
finally
{
if (impersonatedUser != null) impersonatedUser.Undo();
}
現在這裏是有趣/怪異的一部分。在我的網絡中,應用程序可以訪問網絡驅動器,如果我嘗試模擬活動用戶(完全相同的用戶,包括相同的域),它將無法訪問網絡驅動器。
這讓我感到無助,因爲現在我不知道什麼可行,什麼不可行,更重要的是,它會在現場工作嗎?
我錯過了什麼?
編輯:我忘了寫這篇而原本提出質疑:我曾嘗試輸入有效的域名,並沒有工作,所以在那之後我試圖進入空,以獲得相同的用戶名,因爲我會不此代碼(因爲它在我們的域中默認工作)。這沒有幫助,這就是域= null;結束了這個問題。
我敢打賭,超級用戶會爭論相反(因爲這有代碼)。 – Codesleuth 2010-03-23 15:45:55
爲什麼不讓執行此代碼的帳戶對驅動器有讀取權限?然後你的代碼變成這樣:'File.ReadAllBytes(filePath);' – ChaosPandion 2010-03-23 15:47:26
我曾經問過類似的問題;你可能會覺得它很有用:http://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials – Randolpho 2010-03-23 15:47:38