2010-11-15 14 views
0

我想訪問遠程機器上的一些地方。我想要訪問的文件夾完全控制每個人。下面給出的代碼用於訪問網絡路徑。需要在asp.net中訪問遠程機器的建議

System.IO.DirectoryInfo locationInfo = new System.IO.DirectoryInfo(BackupLocationTxt.Text); 
     if (locationInfo.Exists) 
     { 
      // do some operations 
     } 

應用運行正常,如果具有應用程序是否在Visual Studio中運行操作系統Windows XP .The應用還運行正常訪問既在主計算機和遠程計算機。

然後我的問題是,任何一臺機器(服務器和遠程機器)有一個OS更新,然後Windows XP(如Windows 7,服務器2008)locationInfo.Exists總是假。

但如果應用程序在Visual Studio中運行,那麼它做工精細獨立OS

的我搜索了很多網。但沒有找到一個確切的解決方案呢。有人建議冒充。但我不知道如何去做。模擬是我的問題的解決方案?或者有沒有更好的想法?

任何幫助,將不勝感激

+0

還沒回答。 :(。是否有任何問題與我的問題??或需要更多的解釋?? – 2010-11-15 14:39:03

回答

2

嘗試System.IO.Directory.Exists代替。

記住,如果你不至少有隻讀權限的目錄做,Exists方法將返回false

9

你有一個有趣的問題,空。你如何配置你的網站目錄安全?如果啓用了匿名訪問,則根據服務器的操作系統,向Everyone打開的文件夾可能不允許訪問(有關詳細信息,請參閱t his Microsoft KB Article)。

如果該網站以匿名方式運行,則可以更改該網站在IIS管理器中運行的帳戶,也可以啓用模擬。當您在Visual Studio中運行該站點時,該站點將以您的權限運行,因此Anonymous不會成爲問題。

您可以使用以下代碼輸出您的網站正在運行的用戶的身份,以幫助確定發生了什麼。您可能能夠將用戶的網站作爲網絡位置的訪問權限運行,而不需要任何假冒。標籤爲您的網頁,看看你正在運行誰爲:一個ASP添加

lblSomeLabel.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name 

模擬可以打開你到更多的安全風險,所以你應該做讓之前的一些更多的閱讀這種變化 - 但是,用戶你用於模擬不需要是域管理員。在你的情況下,用戶可能只需要具有對網絡位置的完全訪問權限。

你可以閱讀更多關於如何啓用模仿on this Microsoft KB Article。下面是我建議的那個頁面的一些代碼。下面的代碼不是讓你的整個站點以模擬模式運行,而是隻運行你遇到問題的部分。

public void Page_Load(Object s, EventArgs e) 
{ 
    if(impersonateValidUser("username", "domain", "password")) 
    { 
     //Insert your code that runs under the security context of a specific user here. 
     undoImpersonation(); 
    } 
    else 
    { 
     //Your impersonation failed. Therefore, include a fail-safe mechanism here. 
    } 
} 

private bool impersonateValidUser(String userName, String domain, String password) 
{ 
    WindowsIdentity tempWindowsIdentity; 
    IntPtr token = IntPtr.Zero; 
    IntPtr tokenDuplicate = IntPtr.Zero; 

    if(RevertToSelf()) 
    { 
     if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
      LOGON32_PROVIDER_DEFAULT, ref token) != 0) 
     { 
      if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
      { 
       tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); 
       impersonationContext = tempWindowsIdentity.Impersonate(); 
       if (impersonationContext != null) 
       { 
        CloseHandle(token); 
        CloseHandle(tokenDuplicate); 
        return true; 
       } 
      } 
     } 
    } 
    if(token!= IntPtr.Zero) 
     CloseHandle(token); 
    if(tokenDuplicate!=IntPtr.Zero) 
     CloseHandle(tokenDuplicate); 
    return false; 
} 

private void undoImpersonation() 
{ 
    impersonationContext.Undo(); 
} 

此外,雖然serching安全的文章,我發現this StackOverflow question這是值得一讀。

+0

非常感謝你。我試過模擬。你可以看看這個問題http://stackoverflow.com/questions/4213528/dynamically-冒充-A-遠程用戶-C-和-ASP-淨 – 2010-11-19 04:12:19