1,在文件服務器上,創建一個本地帳戶,比如登錄名:upload,密碼:upload,注意在創建的時候選擇“密碼永不過期”,去掉勾選“用戶下次登錄時須更改密碼”的選項;
2,在要共享的文件夾上點右鍵,選擇“屬性”-“安全”,增加upload帳戶可以寫入的權限;
3,在要共享的文件夾上點右鍵,選擇“共享”,共享此文件夾,並在“權限”按鈕點擊後添加帳戶upload可修改;
4,在另外一臺 Web 服務器上,創建登錄名和密碼與上面完全相同的本地帳戶。
5,在web.config裏,啓用模擬:
web.config裏添加的代碼
< identity impersonate="true" userName="upload" password="upload" />
6,在網站文件夾和Temporary ASP.NET Files文件夾上設置帳戶upload讀寫權限
7,在ASP.NET的上傳文件裏寫:
C# 代碼
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(@"\\192.168.3.1\free\" + fileName);
}
8,顯示上傳的文件:
在IIS裏創建虛擬目錄,指向“另一臺計算機上的共享”,“連接爲”輸入上面創建的帳戶名和密碼。即可以http://www.mengxianhui.com/upload/hello.jpg進行訪問。
注意:在VS裏面直接運行可能會報告
Could not load file or assembly 'WebApplication1' or one of its dependencies. 拒絕訪問。
這是因爲你模擬的帳戶沒有權限導致的,你可以發佈到IIS看效果。
下面是一段使用程序進行模擬的方法,出自 http://2leggedspider.wordpress.com/2007/05/28/upload-files-to-unc-share-using-asp-net/ :
C# 代碼
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace FileUploadUNCShare
{
public partial class _Default : System.Web.UI.Page
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
private bool ImpersonateUser(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();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ImpersonateUser("upload", "", "upload"))
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string folderPath = @"\\MyUNCShare\MyFolder\";
string locationToSave = folderPath + "\\" + fileName;
try
{
FileUpload1.PostedFile.SaveAs(locationToSave);
Response.Write("The file has been uploaded.");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
else
{
Response.Write("Please select a file to upload.");
}
UndoImpersonation();
}
else
{
Response.Write("Failed");
}
}
}
}