how to copy file to another server with different user account? - c#

In my project, when user upload a file in the web page which hosted in serverA, the web page should also copy that to serverB. ServerA and serverB is under the same network which admin can access ServerB file from serverA via network drive(//servername/C$/folderpath).
string fileName = "my test.txt";
string sourcePath = #"C:\myFolder";
string targetPath = #"\\vmtest001\C$\myFolder\subFolder";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath)) //error
{
System.IO.Directory.CreateDirectory(targetPath);
}
I have tried to copy that file to ServerB and the error message is
Access to the path '\vmtest001\C$\myFolder\subFolder'is denied.
I see some solutions for that are using a webserivce to move file, could anyone tell me the possibility of that?
Main question: How to copy file between different server?

Add this to your web.config:
<identity impersonate="true" userName="accountname" password="password" />
But this is not recommended.
You can start a service on ServerA, using FileSystemWatcher to monitor the upload files, copy file to serverB in this service。

Related

Run local .html file in same directory as executable in web browser control (visual studio)

I can't seem to run a local .html file on web browser control that's in the same path as the application directory.
I am getting the current application directory, and adding my file name (such as "index.html") at the end of it however it doesn't build successfully.
What's wrong with my code?
string applicationDirectory = AppDomain.CurrentDomain.BaseDirectory
string myFile = Path.Combine(applicationDirectory, "/Media/index.html");
webBrowser1.Url = new Uri("file:///" + myFile);
You don't have to browse with an url , but with a local file path :
WebBrowser1.Navigate("../Media/index.html");

Access to the path is denied while using FileUploadControl

I am trying to save an uploaded file:
string SaveLocation = Server.MapPath(#"~\Data");
FileUploadControl.SaveAs(SaveLocation);
I have provided Full control permission for for both solution and target saving folder for the below user accounts:
Network Service
IIS_IUSRS
ASP.NET/machine
IUSR
I have set <identity impersonate="false" /> in web.config.
I tried all the above but still not able to access the path:
ERROR: Access to the path is denied
Let me know what I need to do to access the path.
FileUploadControl.SaveAs requires a full file name, not only the directory as you pass to it.
string SaveLocation = Server.MapPath(#"~\Data\somefile.png");
FileUploadControl.SaveAs(SaveLocation);
Make sure to change the file path every time you upload a file, or the file will be overwritten.

How to upload file outside the wwwroot folder

I have mvc 3 application to upload the file using third party plugins. I have following path for uploading the file
String path = Server.MapPath("~/App_Data/Uploads/") + categorypath;
It works fine for the above path ~/App_Data/Uploads/ but I want to upload file to the path D drive as follows
String path = Server.MapPath("D:/Uploads/") + categorypath;
But it throws the following exception at that code:
'd:/Uploads/' is a physical path, but a virtual path was expected.
How can I upload file to the D drive.

create folder and copy set of files to local disk

i am trying to create a folder and copy some images into it using c# wpf application.
curName = txt_PoemName.Text;
// Specify a "currently active folder"
string activeDir = #"C:\Program Files\Default Company Name\Setup2\Poems";
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, curName);
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
foreach(DictionaryEntry entry in curPoem){
string newFilePath = System.IO.Path.Combine(newPath, entry.Key.ToString() + Path.GetExtension(entry.Value.ToString()));
System.IO.File.Copy(entry.Value.ToString(), newFilePath, true);
}
i have successfully created the folder and images. and also i can access them via application. but i cant see them in the location on my local disk. when i restart the machine , then application also cant see them.how can i solve this?
Sounds like you have encountered UAC Data Redirection
http://blogs.windows.com/windows/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx
You need to either force the application to run as an administrator.
How do I force my .NET application to run as administrator?
Or not save your data in a sensitive area. I would recommend saving in a subfolder of
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

security issue IIS7.5 / IIS APPPOOL\user not authorized but has full control?

It seems I have a strange issue with security:
I have a website with the following folders:
inetpub\wwwroot
inetpub\wwwroot\readyfordownload
The IIS APPPOOL\Classic user has full access to this 'readyfordownload' folder.
Now I have a console APP that creates a zipfile in the readyfordownload folder. This is done from a c# classlib. Strangely enough, the IIS APPOOL cannot access this file, even though it has full control over the folder. Also, the classlib first creates an xlsx file that is later added to the zip. The APPPOOL user does have access to the xlsx file.
If I run the same function in the C# classlib from a code behind in the website, the same zipfile is created and the IIS APPPOOL user CAN access the file....
Any ideas?
zip is created like this (not the actual code, but it is the same)
http://dotnetzip.codeplex.com/
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("test.xlsx");
zip.Save("MyZipFile.zip");
}
OS is windows 2008 R2 web server
ZIP library is Dotnetzip (Ionic)
Update: I am most interested in why the ZIPfile does not get the rights and the xlsx file does....
Have you tried setting the FileAccessSecurity explicitly? Maybe the files are not inheriting the ACL from the directory.
the apppool user can access the xlsx file because your console creates it directly under readyfordownload folder.
the zip file on the other hand is first created in a temp folder and then copied to your folder. This means that the file permissions are wrongly set on the file.
Make sure IIS_IUSR and DefaultAppPool users have access on your wwwroot.
As scottm suggested change your console code to give permissions to the IUSR and DefaultAppPool users on the zip file. Your code should read like:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("test.xlsx");
zip.Save("MyZipFile.zip");
var accessControl = File.GetAccessControl("MyZipFile.zip");
var fileSystemAccessRule = new FileSystemAccessRule(
#"BUILTIN\IIS_IUSRS",
FileSystemRights.Read | FileSystemRights.ReadAndExecute,
AccessControlType.Allow);
var fileSystemAccessRule2 = new FileSystemAccessRule(
#"IIS AppPool\DefaultAppPool",
FileSystemRights.Read | FileSystemRights.ReadAndExecute,
AccessControlType.Allow);
accessControl.AddAccessRule(fileSystemAccessRule);
accessControl.AddAccessRule(fileSystemAccessRule2);
File.SetAccessControl(path, accessControl);
}
Check Windows EventLog for related errors. For detailed info use ProcessMonitor, so you can see if there is a problem with permissions.
Configure the security of the folder using “advanced securty setting property page”. (Select properties--> security). Also note that the application pool can impersonate the user so that the application may not be serving the request with the identity of the app pool. By default impersonation may not work. You have to set it explicitly in the web config. E.g. <identity impersonate="true" /> or <identity impersonate="true" userName="domain\user" password="password" />
Sriwantha Sri Aravinda

Categories

Resources