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
&
gt;
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
"
);
}
}
}
}