Assume that A is A web server, B is A resource server, and files must be uploaded to server B through the web program on server.
The procedure is as follows:
1. create and share a folder on server B, such as D: \ UploadFiles. Create a virtual directory, UploadFiles, under IIS, pointing to D: \ UploadFiles.
2. Create a new user on server B. For example, the user name is chenya and the password is 123456.
3. Add the user chenya to the UploadFiles permission group and select "full control ". If not, check whether ASP. NET and Users are not added. It is worth noting that we also need to add the chenya user on server A, and the password should be the same, which is used to log on to the ing drive in the code below.
4. If the Administrator password is blank, all users can access the Directory, which is definitely not allowed. Therefore, you must set a password for the Administrator and ensure that the user name and password must be entered (control userpasswords2 in the running state) when using server B. Of course, as server B, the administrator user must set a password, whether or not to upload the file. In this way, an anonymous user needs to enter the authorized user name and password for access, and then the chenya user will be used.
5. Finally, we need to map the UploadFiles on server B to the network drive of server A so that server A can access UploadFiles just like accessing its local hard disk, the following code is introduced into the uploaded cs file:
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 (); // roll back to the account before it is changed
}
// Start upload
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 ");
// Temporarily change to an account with the same user name and password as the network hard disk (this account must have the write permission on the Network Disk). The account with the same account and password is also required on the local machine.
If (impersonateValidUser ("chenya", "192.168.1.100", "123456 "))
{
// Post-Login Password
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 (); // roll back to the account before the change
}
}
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 (); // roll back to the account before it is changed
}
// Start upload
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 ");
// Temporarily change to an account with the same user name and password as the network hard disk (this account must have the write permission on the Network Disk). The account with the same account and password is also required on the local machine.
If (impersonateValidUser ("chenya", "192.168.1.100", "123456 "))
{
// Post-Login Password
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 (); // roll back to the account before the change
}
}
At this time, the configuration has been basically completed, but during the upload, there will still be an error of being unable to access the Temp directory, because during the process from A to B, it is first stored in the Temp Temporary Folder of server A. If you do not have the permission, an error will still occur. Solution: Add Users to the permission Group of the Temp directory and read, write, all modifications are enabled.
Upload the file from a machine. Have you seen the file on server B ???
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/kingya2008/archive/2009/07/12/4341763.aspx