2014-02-14 83 views
2

我有ASP.Net和C#應用程序。我正在將圖像上傳到網站並將它們存儲在C:\Images目錄中,該目錄工作正常。當我將映像保存到C:\Images文件夾並同時將共享驅動器的物理地址(看起來像\\192.xxx.x.xx\some folder\Images)複製(或一些時間移動到)共享驅動器。此驅動器已映射到部署服務器。我正在使用IIS託管的網站。複製文件到共享驅動器在ASP.net失敗c#

問題是與共享驅動器複製。當我從本地計算機(部署站點的位置)使用該站點時,該計算機將文件複製到共享驅動器。但是,當我從另一臺機器(除了部署的服務器)使用該網站時,將該映像保存在C:\Images中,但它不會將該文件複製到共享的驅動器。

下面是我使用

** Loggedon方法的代碼顯示在調試成功。

public static void CopytoNetwork(String Filename) 
    { 
     try 
     { 

      string updir = System.Configuration.ConfigurationManager.AppSettings["PhysicalPath"].ToString(); 

      WindowsImpersonationContext impersonationContext = null; 
      IntPtr userHandle = IntPtr.Zero; 
      const int LOGON32_PROVIDER_DEFAULT = 0; 
      const int LOGON32_LOGON_INTERACTIVE = 2; 
      String UserName = System.Configuration.ConfigurationManager.AppSettings["Server_UserName"].ToString(); 
      String Password = System.Configuration.ConfigurationManager.AppSettings["server_Password"].ToString(); 
      String DomainName = System.Configuration.ConfigurationManager.AppSettings["Server_Domain"].ToString(); 


      bool loggedOn = LogonUser(UserName, DomainName, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref userHandle); 
      try 
      { 
       File.Move(@"C:\Images\" + Filename, updir + "\\" + Filename); 
      } 
      catch (Exception) 
      { 
      } 
      finally 
      { 
       if (impersonationContext != null) 
       { 
        impersonationContext.Undo(); 
       } 

       if (userHandle != IntPtr.Zero) 
       { 
        CloseHandle(userHandle); 
       } 
      } 

     } 
     catch (Exception) 
     { 
     } 
    } 
+0

你可能想利用看看它拋出的異常。 – tzerb

+2

確保正在運行應用程序的用戶具有寫入共享驅動器的適當權限。 – kaptan

+0

它沒有通過任何例外。我也測試過。 –

回答

1

你可以建立一個模擬的用戶類是這樣的:

using System; 
using System.ComponentModel; 
using System.Runtime.InteropServices; 
using System.Security.Principal; 

public class ImpersonatedUser : IDisposable 
{ 
    IntPtr userHandle; 
    WindowsImpersonationContext impersonationContext; 

    public ImpersonatedUser(string user, string domain, string password) 
    { 
     userHandle = IntPtr.Zero; 
     bool loggedOn = LogonUser(
      user, 
      domain, 
      password, 
      LogonType.Interactive, 
      LogonProvider.Default, 
      out userHandle); 

     if (!loggedOn) 
      throw new Win32Exception(Marshal.GetLastWin32Error()); 

     // Begin impersonating the user 
     impersonationContext = WindowsIdentity.Impersonate(userHandle); 
    } 

    public void Dispose() 
    { 
     if (userHandle != IntPtr.Zero) 
     { 
      CloseHandle(userHandle); 
      userHandle = IntPtr.Zero; 
      impersonationContext.Undo(); 
     } 
    } 

    [DllImport("advapi32.dll", SetLastError = true)] 
    static extern bool LogonUser(
     string lpszUsername, 
     string lpszDomain, 
     string lpszPassword, 
     LogonType dwLogonType, 
     LogonProvider dwLogonProvider, 
     out IntPtr phToken 
     ); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern bool CloseHandle(IntPtr hHandle); 

    enum LogonType : int 
    { 
     Interactive = 2, 
     Network = 3, 
     Batch = 4, 
     Service = 5, 
     NetworkCleartext = 8, 
     NewCredentials = 9, 
    } 

    enum LogonProvider : int 
    { 
     Default = 0, 
    } 
} 

當你需要做的將文件複製你做這樣的:

using (new ImpersonatedUser(<UserName>, <UserDomainName>, <UserPassword>)) 
    { 
     DoYourFileCopyLogic(); 
    } 
+0

你通常不會使用, t想要做到這一點 – CodeCaster

+0

@N。Jensen:謝謝你的努力,其他的只是在浪費他們的時間在評論中,不錯的嘗試兄弟! –

+0

@Avi你是對的,我們正在浪費我們的時間 – kaptan

相關問題