ASP.Net如何把檔案從一台伺服器上傳到另外一台伺服器

來源:互聯網
上載者:User

假設A是web伺服器,B是資原始伺服器,檔案要通過A伺服器上的web程式上傳到B伺服器。

步驟如下:

1、在B伺服器上面建立並共用一個檔案夾,比如D:\UploadFiles。在IIS下建立一個虛擬目錄UploadFiles,指向D:\UploadFiles。

2、在B伺服器上建立一個使用者,比如:使用者名稱是chenya,密碼是123456。

3、將使用者chenya添加到UploadFiles的許可權組裡去,並且選擇“完全控制”。如果不行,看看是不是ASP.NET和Users沒有加。值得注意的是,我們還需要在伺服器A上同樣的加上chenya使用者,密碼也要一樣,這是在後面代碼裡用來登入對應磁碟機的。

4、如果Administrator是空密碼,那麼所有的使用者都可以訪問該目錄了,這肯定是不被允許的。所以一定要為Administrator設定一個密碼,並且確保使用伺服器B時必須輸入使用者名稱和密碼(在運行裡輸入control userpasswords2),當然,作為伺服器B而言,管理使用者是必須要設定密碼的,不管我們要不要上傳東西。這樣的話,每當匿名使用者存取時都需要輸入授權的使用者名稱和密碼,這時chenya使用者就派上用場了。

5、最後,我們需要把伺服器B上的UploadFiles映射成伺服器A的網路磁碟機,這樣伺服器A就可以像訪問自己本地的硬碟一樣訪問UploadFiles了,就是在上傳的cs檔案裡引入以下代碼:

view plaincopy to clipboardprint?
public const int LOGON32_LOGON_INTERACTIVE = 2;  
public const int LOGON32_PROVIDER_DEFAULT = 0;  
System.Security.Principal.WindowsImpersonationContext impersonationContext;  
 
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]  
public static extern int LogonUser(String lpszUserName,  
String lpszDomain,  
String lpszPassword,  
int dwLogonType,  
int dwLogonProvider,  
ref IntPtr phToken);  
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
public extern static int DuplicateToken(IntPtr hToken,  
int impersonationLevel,  
ref IntPtr hNewToken);  
 
private bool impersonateValidUser(String userName, String domain, String password)  
{  
    IntPtr token = IntPtr.Zero;  
    IntPtr tokenDuplicate = IntPtr.Zero;  
 
    if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,  
    LOGON32_PROVIDER_DEFAULT, ref token) != 0)  
    {  
        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)  
        {  
            System.Security.Principal.WindowsIdentity tempWindowsIdentity;  
            tempWindowsIdentity = new System.Security.Principal.WindowsIdentity(tokenDuplicate);  
            impersonationContext = tempWindowsIdentity.Impersonate();  
            if (impersonationContext != null)  
                return true;  
            else 
                return false;  
        }  
        else 
            return false;  
    }  
    else 
        return false;  
}  
private void undoImpersonation()  
{  
    impersonationContext.Undo();//回退為未更改前賬戶  
}  
 
//開始上傳  
protected void UploadFile()  
{  
    string m_path = @"http://www.cnblogs.com/ghfsusan/admin/file://192.168.1.100/UploadFiles";  
    m_path = Path.Combine(m_path, "demo");  
 
    //臨時更改為跟網路硬碟相同使用者名稱密碼的賬戶(此賬戶必須在網路盤有寫入許可權)本機也需要同樣帳號密碼的帳戶  
    if (impersonateValidUser("chenya", "192.168.1.100", "123456"))  
    {  
        //登陸後處理密碼  
        if (!Directory.Exists(m_path))  
        {  
            try 
            {  
                Directory.CreateDirectory(m_path);  
                Directory.CreateDirectory(Path.Combine(m_path, "Video"));  
                Directory.CreateDirectory(Path.Combine(m_path, "Html"));  
                Directory.CreateDirectory(Path.Combine(m_path, "Doc"));  
            }  
            catch (Exception e)  
            {  
                Response.Write(e.Message);  
            }  
            FileUpload1.SaveAs(@"http://www.cnblogs.com/ghfsusan/admin/file://192.168.1.100/UploadFiles/demo/newfile.rar");  
            undoImpersonation();//回退為未更改前賬戶  
        }  

public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
System.Security.Principal.WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);

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

    if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
    {
        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
        {
            System.Security.Principal.WindowsIdentity tempWindowsIdentity;
            tempWindowsIdentity = new System.Security.Principal.WindowsIdentity(tokenDuplicate);
            impersonationContext = tempWindowsIdentity.Impersonate();
            if (impersonationContext != null)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    else
        return false;
}
private void undoImpersonation()
{
    impersonationContext.Undo();//回退為未更改前賬戶
}

//開始上傳
protected void UploadFile()
{
    string m_path = @"http://www.cnblogs.com/ghfsusan/admin/file://192.168.1.100/UploadFiles";
    m_path = Path.Combine(m_path, "demo");

    //臨時更改為跟網路硬碟相同使用者名稱密碼的賬戶(此賬戶必須在網路盤有寫入許可權)本機也需要同樣帳號密碼的帳戶
    if (impersonateValidUser("chenya", "192.168.1.100", "123456"))
    {
        //登陸後處理密碼
        if (!Directory.Exists(m_path))
        {
            try
            {
                Directory.CreateDirectory(m_path);
                Directory.CreateDirectory(Path.Combine(m_path, "Video"));
                Directory.CreateDirectory(Path.Combine(m_path, "Html"));
                Directory.CreateDirectory(Path.Combine(m_path, "Doc"));
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
            FileUpload1.SaveAs(@"http://www.cnblogs.com/ghfsusan/admin/file://192.168.1.100/UploadFiles/demo/newfile.rar");
            undoImpersonation();//回退為未更改前賬戶
        }
}

這時已經基本上完成了配置了,但在上傳的時候,還是會出現無法訪問Temp目錄的錯誤,因為檔案從A到B的過程中,是先存入伺服器A的Temp臨時檔案夾的,如果沒有許可權,依然會出錯,解決方案:將Users加入到Temp目錄的許可權組,把讀取,寫入,修改都開啟即可。

隨便從一台機器上傳一下,是不是已經在B伺服器上看到你傳的東西了???

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/kingya2008/archive/2009/07/12/4341763.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.