1. Create a local account on the file server, such as the login name: Upload, password: Upload. Note that when creating the account, select "the password will never expire ", deselect the "Change Password Upon next login" option;
2. Right-click the folder to be shared and choose "properties"> "security" to add the write permission for the upload account;
3. Right-click the folder to be shared, select "share", share the folder, and click "permission" to add the account upload to be modified;
4. On another Web server, create a local account with the same login name and password as above.
5. Enable simulation in Web. config:
Code added in Web. config <
Identity
Impersonate
= "True"
Username
= "Upload"
Password
= "Upload"
/>
6. Set the account upload read and write permissions in the website folder and temporary ASP. NET Files folder.
7. Write in the ASP. NET upload file:
C # Code protected
Void
Button#click (
Object
Sender, eventargs E)
{
String
Filename
=
System. Io. Path. getfilename (fileupload1.postedfile. filename );
Fileupload1.saveas (
@"
// 192.168.3.1/free/
"
+
Filename );
}
8. Display uploaded files:
Create a virtual directory in IIS, point to "share on another computer", and "Connect as" Enter the account name and password created above. Http://www.mengxianhui.com/upload/hello.jpg.
Note:
Running directly in Vs may report
Cocould not load file or assembly 'webapplication1' or one of its Dependencies. access is denied.
This is because your simulated account has no permissions. You can publish it to IIS to see the effect.
The following is a simulation method using the program, from the http://2leggedspider.wordpress.com/2007/05/28/upload-files-to-unc-share-using-asp-net:
C # code
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
Button#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
"
);
}
}
}
}